Url Rewriting ASP.NET - Some images refuse to load - c#

I have recently implemented UrlRewriter (http://urlrewriter.net) on my website and am having some issues.
I am implementing it so the page requests are extension-less. For example, www.example.com/my-cool-product, redirects to www.example.com/Product.aspx?id=1. This works fine.
The problem I am having is that, some of my site images are refusing to be served as static content. If I put the path to some of images on my site, they are served up right away (as static content), but some images try to route through the .NET pipeline.
For example, www.example.com/Asset/Image/Image.png returns a 404 as it is trying to hit up www.example.com/Asset/Image/Default.aspx.
Can anyone shed any light on why this is happening for some images and not for others?

What version of IIS are you using? You may need:
<modules runAllManagedModulesForAllRequests="true">
In your web.config <system.webServer> block
Or set a <base> url in your page head

Related

How to I make global.asax run when the URL has a dot?

I have a website running on IIS 7.5 which I can only access using FTP. (So I won't be able to use IIS Manager.)
I want to reorganise the URL structure, so I've modified the Global.asax code to look for the old URLs and Response.Redirect them to their new location. This works great for some of the URLs but not all.
With some experimentation, I found that when the old URL has a dot (eg, http://example.com/hello.html) then my global.asax code doesn't run. If I remove the file extension part of the URL, the global.asax code runs fine.
Those URLs with .html etc on the end are already out there and I can't change them now.
What do I need to do please?
What's happening is that IIS thinks your request for "/hello.html" is a static file and tries to serve it up directly without handing it off to your ASP.ENT application (before it gets to the routing in your Globals.asax file).
To change this behavior, you need to tell IIS to send these requests to your ASP.NET app instead of handling them itself. You can do this in your web.config file:
<system.webServer>
<handlers>
<add name = "LetMeHandleMyOwnStaticContent" path ="/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersion4.0"/>
</handlers>
</system.webServer>
Note, this will make you handle ALL of the static files (be prepared to handle the .js files, .css files, .html files, .png files, etc.) through your app. You can manipulate the path and verb to narrow down what you want to serve through your app. You can add multiple filters by repeating the "add" element (just use a unique name for each path).

how to redirect url request to https in webforms proj

Any thoughts on why implementing Application_BeginRequest to check for the Url scheme (to replace http with https within the requested url and redirect) coupled with
<modules runAllManagedModulesForAllRequests="true">
in the config file of a webforms solution is not enough to achieve this purpose?
So far it works only for urls like: www.mysite.com/path1, not for www.mysite.com/path1/file.aspx. here it says it didn't find the file. How can i force it to switch to https before actually looking for a file?
You can do redirects on global.asax file on Application_BeginRequest()
This is one of the ways you can go about it.

Capture request static file in asp.net

How can I capture and handle static file requests with asp.net?
I've tried the "IModule" and "Global.asax" with BeginRequest but
the request is not captured.
Is there some setting in IIS to direct some Component (IModule / Handler / Global.asax / DLL) in asp.net?
Or some way to direct requests to some DLL made with C #?
For example:
"get http://localhost/myapp/report/report1.html"
How can I capture this request?
I could not do, get in asp.net.
I want to be able to check the permission, and can handle the file, and handle the response.
The simplest way if you are running the Application Pool in Integrated mode is to tell IIS to send all requests to the "Managed Handlers".
In your web.config you should have a <system.webServer> element which may have a modules element - update that to:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
[...]
With that in place you'll be able to use all the standard features of ASP.NET including authentication control on paths.
Note that this will send all requests through your application, so you'll see requests for .css and .js files in there as well so if you do try locking everything down you'll need to leave those open.
Yes, IIS has such setting, Handler Mapping.
The setting determines with handlers would be called for different file extensions. Set the ASP.NET handler to desired extensions, and the you can use ASP.NET features (IHttpModel and other) to capture.

ASP.Net MVC returns 404 for URLs with Action parameters but not for default route

I am using a WebForms application with some MVC components added in. The idea is to move more and more of the app over to MVC but there is no way it can be transitioned all at once. For various reasons I cannot control it must use ASP.Net 2.0 and MVC 2.0 since those are what ship built-in. I must also support IIS 6 and IIS 7.
First, I am well aware of the problems with extensionless routing and I am not attempting to use it so there are no issues with wildcard mappings, etc. I first attempted to use my routes ala "{controller}.aspx/{action}/{id}" but after banging my head on the wall I switched to "{controller}.mvc/{action}/{id}" but am having the same issues.
Second, I cannot get this to work even in IIS 7 Integrated mode on my dev machine, let alone Classic mode or IIS 6. It all runs correctly under Cassini but once I deploy to IIS 7 the MVC components break. Since this is on my dev machine I know ASP.Net is registered with IIS correctly and I can see all the inherited HTTP handlers in the control panel (eg: ASPX maps to PageHandlerFactory).
Symptoms:
All ASPX WebForms requests work perfectly.
An MVC requests to just the controller with no action/id specified get routed to MVC and execute properly as well.
Any request to an MVC route with an action or id immediately returns a 404. It is as if IIS thinks the ".mvc" extension is part of the folder path so it ignores the HTTP handler and returns a 404.
In other words:
/app/WebForm.aspx - HTTP 200 OK, executes WebForm.aspx.cs code-behind
/app/Fancy.mvc - HTTP 200 OK, executes /Controllers/FancyController.cs, Index method
/app/Fancy.mvc/DoThingy - HTTP 404 NOT FOUND, even though FancyController has DoThingy method
Bad Solutions: I have tried things like setting runAllManagedModulesForAllRequests but not only is that bad for performance it also breaks my Web Forms as well. Even when I set it to ignore all routes with .ASPX in them they still break.
I cannot use wildcard mapping so that is no help.
Other Details:
I setup my HTTP Handler in web.config/system.WebServer. It is the first handler listed.
<add name="MvcRoutingHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Routing.UrlRoutingHandler, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" resourceType="Unspecified" />
It doesn't seem to matter what handler I specify or what options - IIS just doesn't seem to be examining any of this configuration (again because it seems to think the Fancy.mvc part of the path is a directory name, doesn't find that directory, then bails).
My routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspq/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.MapRoute("Default",
"{controller}.mvc/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
Update: I tried using IIS 7 Classic mode; I then added a .mvc mapping in the IIS Manager pointing at the asp_net ISAPI dll and got the same result
OK just so others don't look foolish, it turns out that this was a problem with URLs... the URL was being sent to the root of the site, not the app directory. I should have checked that to begin with. I didn't think this was the problem because when I manually typed the URL into the address bar it worked on the Index but the action method required HttpPost, so it was a combination of factors that made the script and manually-entered addresses spit out the same error message.
For anyone else mixing WebForms and MVC, double and triple-check that your URLs are correct. Here is some code I am now using on the WebForms master page so my client-side JS can know where to route MVC requests:
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
if (baseUrl.EndsWith("/")) baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
baseUrl = baseUrl + ResolveUrl("~/");
Page.ClientScript.RegisterHiddenField("BaseUrl", baseUrl);

IRouteHandler in Web Forms: Routing requests that require HttpContext.User

I'm trying to add a pretty basic route to an Asp.Net Web Forms app (running under IIS 7, integrated mode): for requests coming to http://mydomain.com/foo/ I would like to show the results of a dynamic page (http://mydomain.com/foopage.aspx).
I've created a RouteHandler that does all this and it seems to be routing correctly.
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var page = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/foo.aspx", typeof(MyApp.Web.Foo)) as MyApp.Web.Foo;
return page as IHttpHandler;
}
The problem is, inside my RouteHandler's GetHttpHandler method, all the instances of the current user (requestContext.HttpContext.User, System.Web.HttpContext.Current.User) are null. Sadly, foo.aspx needs to know what the current user is (for login controls, role stuff, etc), so rendering the page is throwing null reference exceptions. My guess is that these route handlers are firing off before Asp.Net gets the chance to wire up the HttpContext with user info. Any idea of a work-around?
PS - I realize this can be accomplished by doing a Server.Transfer in a page at http://mydomain.com/foo/default.aspx. I'd like to use routing for this sort of thing rather than having a bunch of useless folders cluttering things up.
Thanks!
See the answer to this question, very similar.
I managed to figure this one out myself.
Much like this question, my routes were working just fine when the route origin ended in .aspx (http://mydomain.com/foo-origin.aspx), but failed when they did not (http://mydomain.com/foo-origin/).
The MSDN article on setting up routing with web forms tells you to make a few changes to web config, but leaves out that you need to set runAllManagedModulesForAllRequests to true in the modules node:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</system.webServer>
</configuration>
Now it works swimmingly.

Categories

Resources