<h3 dir="auto">Describe the bug</h3>
<p dir="auto">Mounting StaticFiles with an APIRouter doesn't work.</p>
<h3 dir="auto">To Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from typing import Any
from fastapi import FastAPI, Request, APIRouter
from fastapi.staticfiles import StaticFiles
from fastapi.testclient import TestClient

router = APIRouter()

@router.get(&quot;/&quot;)
async def foo(request: Request) -&gt; Any:
    # this raises starlette.routing.NoMatchFound
    return request.url_for(&quot;static&quot;, path=&quot;/bar&quot;)

app = FastAPI()

router.mount(&quot;/static&quot;, StaticFiles(directory=&quot;.&quot;), name=&quot;static&quot;)
# uncomment to fix
# app.mount(&quot;/static&quot;, StaticFiles(directory=&quot;.&quot;), name=&quot;static&quot;)

app.include_router(router)

client = TestClient(app)
client.get(&quot;/&quot;)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">Any</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>, <span class="pl-v">Request</span>, <span class="pl-v">APIRouter</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span>.<span class="pl-s1">staticfiles</span> <span class="pl-k">import</span> <span class="pl-v">StaticFiles</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span>.<span class="pl-s1">testclient</span> <span class="pl-k">import</span> <span class="pl-v">TestClient</span>

<span class="pl-s1">router</span> <span class="pl-c1">=</span> <span class="pl-v">APIRouter</span>()

<span class="pl-en">@<span class="pl-s1">router</span>.<span class="pl-en">get</span>(<span class="pl-s">"/"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">foo</span>(<span class="pl-s1">request</span>: <span class="pl-v">Request</span>) <span class="pl-c1">-&gt;</span> <span class="pl-v">Any</span>:
    <span class="pl-c"># this raises starlette.routing.NoMatchFound</span>
    <span class="pl-k">return</span> <span class="pl-s1">request</span>.<span class="pl-en">url_for</span>(<span class="pl-s">"static"</span>, <span class="pl-s1">path</span><span class="pl-c1">=</span><span class="pl-s">"/bar"</span>)

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()

<span class="pl-s1">router</span>.<span class="pl-en">mount</span>(<span class="pl-s">"/static"</span>, <span class="pl-v">StaticFiles</span>(<span class="pl-s1">directory</span><span class="pl-c1">=</span><span class="pl-s">"."</span>), <span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">"static"</span>)
<span class="pl-c"># uncomment to fix</span>
<span class="pl-c"># app.mount("/static", StaticFiles(directory="."), name="static")</span>

<span class="pl-s1">app</span>.<span class="pl-en">include_router</span>(<span class="pl-s1">router</span>)

<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-v">TestClient</span>(<span class="pl-s1">app</span>)
<span class="pl-s1">client</span>.<span class="pl-en">get</span>(<span class="pl-s">"/"</span>)</pre></div>
<ul dir="auto">
<li>Execute the script, raises NoMatchFound</li>
<li>Uncomment line to mount with app instead</li>
<li>Executes as expected</li>
</ul>
<h3 dir="auto">Expected behavior</h3>
<p dir="auto">I can use APIRouter() as if it was a FastAPI() as noted in the docs.</p>
<p dir="auto"><strong>Description</strong></p>
<p dir="auto">This is half a question on conceptual ideas, and half a feature request maybe?</p>
<p dir="auto">I was wondering if anyone here has any thoughts on the following scenario.</p>
<p dir="auto">Say I'm making a profile API.  When posting a new profile, you use this model</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class Profile(BaseModel):
  first_name: str
  last_name: str
  title: str = None
  age: int = None
  tagline: str = None"><pre class="notranslate"><code class="notranslate">class Profile(BaseModel):
  first_name: str
  last_name: str
  title: str = None
  age: int = None
  tagline: str = None
</code></pre></div>
<p dir="auto">First name and last name are understandably required fields, because we have to have <em>something</em> for a profile.</p>
<p dir="auto">So now I want to add a patch endpoint, where you can just edit a single field.  For that we want to enable the following request:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="patch service.com/profile/1234
{
  &quot;tagline&quot;: &quot;I am so cool&quot;
}"><pre class="notranslate"><code class="notranslate">patch service.com/profile/1234
{
  "tagline": "I am so cool"
}
</code></pre></div>
<p dir="auto">But I haven't given a first and last name, so obviously this endpoint can't use the Profile model as its body model.</p>
<p dir="auto">What is the best approach for this?  I can think of a variety of options, but nothing really <code class="notranslate">sparks joy.</code></p>
<p dir="auto">Obviously one approach is to just make a different model for patch.  This is certainly the most straightforward, but across a large service you might end up with a <em>lot</em> of duplicate models.  And you'll have to manually maintain similarities between the two <g-emoji class="g-emoji" alias="nauseated_face" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f922.png">🤢</g-emoji></p>
<p dir="auto">Another thought I had (not sure how easy this would be with Fast built in behaviors?  I might be missing something), was to maybe bring the method <code class="notranslate">POST v PATCH</code> into a field on the profile model, and write a validator that is dependent on the value of that method field...but that is definitely a bit hacky <g-emoji class="g-emoji" alias="grimacing" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f62c.png">😬</g-emoji></p>
<p dir="auto">There are likely lots of other ideas, just curious what the thoughts are from other users of this API (and or the creator)</p>