ASP.NET Routing - How to respond 404 for .aspx requests - c#

I am using routing in webforms to clean up urls. I store all my .aspx files in the /Pages directory and route to them with clean URLs. As it stands I can still access these files by navigating to /Pages/Home.aspx for example. How can I either throw a 404 or redirect to a different page in my routing table? Here is what I have tried:
routes.RouteExistingFiles = true;
routes.Ignore("{resource}.aspx/{*pathInfo}");
routes.Ignore(#"*\.aspx");
routes.MapPageRoute("Home", "", "~/Pages/Home.aspx");
This isn't working at all, and I could use some advice.
If I try accessing a physical file on my MVC site, I get the following:
Object reference not set to an instance of an object.
Can I do this on webforms?

routes.RouteExistingFiles = true;
routes.MapPageRoute("Route1", "Pages/{page}.aspx", "~/404.aspx");
routes.MapPageRoute("Route2", "Pages/{folder}/{page}.aspx", "~/404.aspx");
Instead of Ignoring the route, you need to map it to a page that throws a 404 instead of a response. Hopefully this helps someone else!

Related

I need to redirect the /default page of my application(Sitecore) to another page of my hosted site

1.I have tried adding the IIS Rewrite Rule which works for local environment but not in the higher environments.
2.Sitecore redirect settings is also not working.
3.Currently (localhost)/default loads the homepage and I need this to get redirected to another page of my site.
Can somebody help me to achieve this without overriding the HttpBeginRequest Processor?
public IActionResult Privacy(){return Redirect("https://google.com"); }
As you have an MVC project, requests will all route to a controller action, you can set the redirection in the corresponding action.

site will not redirect to custome page and throw 404 error in asp.net site

I don't know this question is already asked or not. But i am stuck in one problem.
I have one CMS application in that some page are static and some are dynamic.
When i access url using http://abc.com/abc.aspx
Now in this case if abc.aspx is cms page then it will redirect me to that page but if it is not cms page then this will redirect me to the my custom page http://abc.com/page-not-found.aspx
Now my question is that if i write only http://abc.com/abc then it will didn't redirect me to the http://abc.com/page-not-found.aspx but it will throw me the error 404 page or directory not found.
Now I have to check two things
1) if any custome page is not there then it will display http://abc.com/page-not-found.aspx
2) when http://abc.com/page then it will also redirect me to http://abc.com/page-not-found.aspx
Please help me our from this. This will work fine in my local but only problem in live environment.
Thanks in advance.
Regards
AB Vyas
You may need to look into Handlers in your web.config .
http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx

Routing in Asp.Net 4.0

I have successfully setup routing in Asp.Net 4.0 webforms. I have set up:
routeCollection.MapPageRoute("Default Page", "Default/{ProductName}/{CategoryName}", "~/Default.aspx");
However, problem is even though the user browses to default.aspx, the page still shows up. How can I avoid this? I want only the MapPageRoute to work. I want that when user browses to default.aspx some error should be thrown or 404 page should be shown etc. In short I do not the user to browse through default.aspx. How can I do this?
Thanks in advance :)
you can handle this issue through two ways.
In global.asax in Request_Start event check that if the requested url end by .aspx redirect to error page.
Use Url rewriter, by regular expression identify the wrong requests and redirect them to custom error page.
Never used routing in WebForms myself, but have you tried looking at the Request.Url? If that ends with/contains you could handle this by redirecting to an error page.

URL Routing Encoded URLs in web application

My goal is to move away from an ISAPI filter that was set up and instead do all the rewriting/routing at the application level.
I have URLs like: product.aspx/2008C20080929.htm
I can correctly route those pages using the following RouteTable.Routes.MapPageRoute:
RouteTable.Routes.MapPageRoute("testRoute", "product.aspx/{page}", "~/routeTest.aspx");
However, some of the other URLS are "encoded", example:
product.aspx/%255COH%255C2008%255C20080929.htm
I am unable to route these pages with the previously stated route. Is this not possible? I'm getting 400 bad requests.
Instead of using URL Routing, I used IIS7's Rewrite Module
URL Rewrite

Are ASP.NET MVC routes cached?

I added a route to my asp.net mvc application to handle some json requests I need to do. This works great on my development pc, however when I installed in QA, the route isn't working at all. I tried to physically type in the address and get a "Bad Request". Can anyone assist with this? I have restarted IIS to try to clear any cache but still no luck. First time, I've seen this as I've made several changes to routes before.
routes.MapRoute(
"FsrProblemTypesByEquipment",
"Fsr/ProblemTypesByEquipment/{equipmentID}",
new {controller = "Fsr", action = "ProblemTypesByEquipment", equipmentID = ""});
Do you still get a "bad request" if you take out the new route entry, but use the same test URL? If it is really a problem with your routing table, then your catchall route should get the request.
My guess is that the request is never getting to your routing table; IIS is catching the request before it gets passed to your application. If that is true, then it is a configuration problem in IIS, or IIS is unhappy with the construction of the URL for some reason.
See this forum post for more information:
http://forums.asp.net/p/1458130/3343674.aspx

Categories