How can I use MVC5 attribute based routes with point / period in them?
I have a basic MVC 5.2 application with attribute based routes included (call routes.MapMvcAttributeRoutes();
from the provided method RouteConfig.RegisterRoutes(RouteCollection routes)
).
HomeController has the following:
[Route("hello.txt")]
[Route("hellotxt-testing")]
public ActionResult ShowFile()
{
return Content("Hello world");
}
I can successfully access /hellotxt-testing
(attribute based routing check works correctly):
But I am getting 404 pages if I access /hello.txt
:
Unlike when I go to /hello404
where I get another 404 page:
I'm guessing the second 404 page is from ASP.NET and the first is from IIS, which doesn't even submit the ASP.NET request?
What should I do to ensure ASP.NET receives these urls with periods in them? Are there other "special" characters that can cause the problem as well?
source to share
The difference between the errors you see is that when you try to access /hello.txt
, IIS tries to get to the static resource file located on the server, on the other hand /hello404
it is just an unknown route from which the 404 is called.
To make sure I'm correct, try adding a slash to the end and /hello.txt/
you should now get a 404 error.
There are several ways to solve this problem:
First, you can try
<modules runAllManagedModulesForAllRequests="true">
but this setting, called RAMMFAR, hurts the performance of the web application and has other consequences.
Second you can create a rule for IIS REWRITE url to always add trailing slash for every incoming url p>
Third go Dots in url result in 404 with ASP.NET mvc and IIS and
<add name="ApiURIs-ISAPI-Integrated-4.0"
path="/people/*"
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
but you didn't like it.
Fourth, try to create your own HTTP handler to add a trailing slash to all requests, like in this section Add a trailing slash at the end of each URL?
I would go with option two if you need this to work in many places in your application.
But I think the best one is the third one . Such a dotted URL is not user-friendly and violates SEO guidelines. You should avoid them on public websites. So, assuming you only need this for one controller and route, this is the fastest and cleanest option.
source to share
We had the same problem (albeit not with attribute routing) and it started showing up in MVC 4 and has continued in every release since then.
One of our project members discovered a solution. I'm afraid I can't explain why, but deleting and then replacing UrlRoutingModule
fixes the 404 issue when using a route with .
in it.
<configuration>
<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
</modules>
</system.webServer>
</configuration>
This was discovered about 2 years ago, and so far we have not experienced any negative impacts caused by this change.
BTW - If anyone has an explanation as to why this works, please share.
source to share