Why does MvcHttpHandler is mapped from *.mvc? - asp.net-mvc

By default the web.config file for MVC project have the following element:
<handlers>
<remove name="MvcHttpHandler"/>
<add name="MvcHttpHandler" preCondition="integratedMode"
verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler"/>
</handlers>
My problem is that my site returns 404.14, after knocking out all the usual suspects I changed the path (form the snippet above) attribute in the web.config to be "*" and voilà! MVC handler kicks in.
So my question is how does *.mvc even suppose to work? I mean my urls are http://mysite.com/home/index (or even only http://mysite.com/) there is no *.mvc in them.
Am I missing something?

By changing the path you are telling the routing engine to add the .mvc extension to the Url. You probably do not have the .mvc extension mapped in IIS and receive an error.
See here on information about IIS and MVC especially if you are using IIS 6.0:
http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx

Related

HttpHandler not working

I am trying to integrate the FileUpload project from vid.ly to my web application which is in MVC ASP .NET 4 framework using Razor engine.
I have an HttpHandler under App_Code.
Progress.cs
public class Progress : IHttpHandler
{
...
}
I added this in RegisterRoutes() called from Globals.asax:
routes.IgnoreRoute("Progress.progress");
And added these lines in system.web section in web.config:
<httpHandlers>
<add verb="*" path="*.progress" type="MyNamespace.Progress"/>
</httpHandlers>
When I try to open http://localhost:39234/Progress.progress, I get a HTTP Error 404.0 - Not Found error with below detailed information:
Requested URL http://localhost:39234/Progress.progress
Physical Path c:\users\xxxxx\documents\visual studio 2012\Projects\SolutionName\ProjectName\Progress.progress
Seems like something is wrong with the mapping. Does anyone have any idea what I'm missing?
I finally figured out what the problem was. I had to put the mappings in system.webServer section in this format:
<add name="Progress" verb="*" path="*.progress" type="MyNamespace.Progress,MyNamespace"/>
Or, set the Action Build Path to "Content" for the Progress.cs files so that it doesn't get compiled in the MyNamespace.dll

URL Pattern Routing MVC 3

I want to know how to have something like this in the url in asp.net MVC
/Article/12.20.2013
I tried below and its working fine for /Article/12-20-2013 but not for /Article/12.20.2013. I have below in the Global.asax
routes.MapPageRoute("Blog",
"/Article/{entryDate}",
new {controller = "Article", action = "Entry")};
I also tried something like below
routes.MapPageRoute("Blog",
"/Article/{month}.{Date}.{year}",
new {controller = "Article", action = "Entry")};
but no luck..
Please guide me with some sample.
I believe the issue is that IIS might be treating ".2013" as a file extension and trying to find a handler for it. What we need to do is to have MVC process all requests.
If you are on IIS 6 this you will need to do a wildcard mapping to aspnet_isapi.dll. If you are on IIS 7 you can set the runAllManagedModulesForAllRequests="true":
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
You need to change web.config to force every url starting with Article to be treated as a MVC url
<system.webServer>
<handlers>
<add name="UrlRoutingHandler"
type="System.Web.Routing.UrlRoutingHandler,
System.Web, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
path="/Article/*"
verb="GET"/>
</handlers>
</system.webServer>
Then your routing should be working fine.

why is the httphandler not running

I have written an httpHandler for an ASP.NET MVC4 site that generates an an image. The ProcessRequest function is not being called. Any thoughts on why?
MVC4, IIS Express, Windows 8 Pro
Web.config > system.webServer
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="TextImage" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="TextImage" path="textimage/*.png" verb="*" resourceType="Unspecified" type="MultiStepUI.TextImageHandler, MultiStepUI_MOBETTER" />
</handlers>
</system.webServer>
usage
<img src="/textimage/step1.png?q=Step 1&c=404040&w=30&h=250&z=12" />
The answer can be found on the web if one just knows what to look for.
MVC routing engine tries to map all requests to a controller - which is not what we want in this case. In addition to registering the handler in Web.config we need to tell the MVC route engine to ignore the httpHandler path so that the ASP.NET engine can handle its routing.
I've chosen to use the example from Phil Haack
To combat link rot this is an excerpt from the article
By default, ASP.NET Routing ignores requests for files that do not
exist on disk. I explained the reason for this in a previous post on
upcoming routing changes. Long story short, we didn’t want routing to
attempt to route requests for static files such as images.
Unfortunately, this caused us a headache when we remembered that many
features of ASP.NET make requests for .axd files which do not exist on
disk.
To fix this, we included a new extension method on
RouteCollection, IgnoreRoute, that creates a Route mapped to the
StopRoutingHandler route handler (class that implements
IRouteHandler). Effectively, any request that matches an “ignore
route” will be ignored by routing and normal ASP.NET handling will
occur based on existing http handler mappings. Hence in our default
template, you’ll notice we have the following route defined.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
This handles the
standard .axd requests. However, there are other cases where you might
have requests for files that don’t exist on disk. For example, if you
register an HTTP Handler directly to a type that implements
IHttpHandler. Not to mention requests for favicon.ico that the browser
makes automatically. ASP.NET Routing attempts to route these requests
to a controller. One solution to this is to add an appropriate ignore
route to indicate that routing should ignore these requests.
Unfortunately, we can’t do something like this:
{*path}.aspx/{*pathinfo}
We only allow one catch-all route and it must
happen at the end of the URL. However, you can take the following
approach. In this example, I added the following two routes.
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
What I’m doing here is a
technique Eilon showed me which is to map all URLs to these routes,
but then restrict which routes to ignore via the constraints
dictionary. So in this case, these routes will match (and thus ignore)
all requests for favicon.ico (no matter which directory) as well as
requests for a .aspx file. Since we told routing to ignore these
requests, normal ASP.NET processing of these requests will occur.
The previous answer is correct, but the article has been edited from the excerpt placed here. The ignore statements should read:
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
Note the '*' in the {*allaspx} and {*favicon} strings that is missing in the original.
I could not get it to work until I followed the link and followed the example in the updated article.

requests with extensions throwing 404 in asp.net mvc

All my requests ending with any extension like .jpg, .gif are throwing 404. The content for these requests are served from database. It works if i append a trailing backslash to the request.
http://www.example.com/1.jpg doesn't work
http://www.example.com/1.jpg/ Works
i have runAllManagedModulesForAllRequests set to true but i think it's not used in 7.5.
This also work perfectly fine in my box but doesn't in a test box.
I edited my web.config and added the following to get .json to work.
<handlers>
...
<add name="MyJsonHandler" path="*.json" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
You can do this manually in your web.config or use IIS to manage it in the HTTP Handlers for the web site you are configuring.

ASP MVC Route issue with dot "."

I have this route:
file{FileId}/{name}
It works perfectly unless name has a dot in it.
For example, these work:
file1/blah, file90/foo -
but it doesn't:
file1/blah.doc
All I get in this case is 404 error. Seems like it looks for the actual file blah.doc instead of use routing system.
This problem happens only in production server. I've even tried
httpRuntime relaxedUrlToFileSystemMapping="true"
but it didn't help.
Everything after the '.' is the file extension. If that extension isn't mapped to ASP.NET, it won't get handed off to the ASP.NET handler. IIS looks for a static file instead. So you need to add a handler for your case (web.config), then your route will be able to catch the request.
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="FileHandlerDot" verb="GET" path="file*/*.*" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</validation>

Resources