MVC C# Bad URL handling with multiple parameter - c#

I am new to MVC. Actually, I am trying to handle the bad request for my application. I have added Application_Error method in Global.asax and also created a controller to handle the 404-page error. Now this works fine when my URL is like following.
https://localhost:44397/t/t/t
It shows proper 404 page I have set for the bad URL
But It shows again 404 page when my URL is something like following
https://localhost:44397/t/t/t/e
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Can anyone help me how I can restrict above URL? Following is my route.config code.
routes.MapRoute(
"ErrorHandler",
"ErrorHandler/{action}/{errorMSG}/",
new { controller = "ErrorHandler", action = "Index", errMsg = UrlParameter.Optional }
,
namespaces: new[] { "MyProject.Web.Controllers" }
);

You need to register an additional route at the end of all valid routes which would match any URL. Sample code below. This will handle all invalid requests to your application.
routes.MapRoute(
"AllInvalidRoutes",
"{*.}",
new { controller = "InvalidRoutesController", action = "Index" }
);

Related

ASP.NET WebForms - routing gives 'page not found error '

I am using asp.net routing and getting a 404 error.
If I do not use an id value then I can see page.
But I want to pass iddata with route and getting 404 Page Not Found error.
Not sure why we see error when we want to use parameters.
With Parameters (404 File or Directory not found error)
routes.MapPageRoute( "Product", "product/{id}", "~/Pages/Product.aspx" );
Without Parameters (Page Works)
routes.MapPageRoute( "Product", "product", "~/Pages/Product.aspx" );
UPDATED NOTE: I have tested all routes if i add product/{id}, contact/{id} or etc. It causes all the same 404 error.
URL: test.com/product > WORKS
test.com/product/{id} > NOT FOUND
try this with * query values
routes.MapPageRoute(
"Product",
"product/{Id}/{*queryvalues}",
"~/Pages/Product.aspx"
);

MVC routing - specify id without action

I would like to specify an id without having to include the index action.
Here is what I have tried:
routes.MapRoute("Upload", "Upload/{id}",new { controller = "Upload", action = "Index" });
This gives a 404 error. The url will be something like site.com/Upload/123
Probably you need to put the code before the Default route. MVC check routes from the any route after default won't work

How to handle a virtual path created by proxy server in MVC route?

I am fairly new to MVC. Recently I developed a site and hosted it behind a proxy server. I access my site using internalhostname/site/{controller}/{action}/{id} for test, which works fine.
However, when my users are connecting the site, they would use an external url like this: externalhostname/apps/site/{controller}/{action}/{id}. Now when my views attempt to initiate a call to the controller according to the default route config, the URLs being generated become externalhostname/site/{controller}/{action}/{id}, notice "/apps" is gone.
Well, this is a known problem to me. Because when creating the URL, host name does not include "/apps". In other sites created in regular ASP.NET page, I would simply hijack the URL creation and replace host with host/apps, that can fix the issue. But I don't know how to do this in MVC world.
Also I am using Telerik controls (for MVC) that also initiate requests by controller and action, which lead to the wrong URL eventually.
The route config is default. I did try to change the rule but all of those only affect the url format after . Nothing could allow me to change the behavior ahead of it.
I have been struggling for days and couldn't see a way out. Appreciate for some advice. Thank you.
I cannot change the proxy rule. That is not an option to me.
Environment: MVC 4, IIS 7.0, if these matter.
Try to add new entry in your route config.
routes.MapRoute(
"Default2", // Route name
"apps/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Take note of the order.

MVC cache error page

When there is a 404 error, my application redirects to page http://localhost:41083/hata/page-not-found
Once my page http://localhost:41083/iletisim/musterimiz-olun redirects to 404 page, even though I fix the error it still goes to 404 page, it does not hit the controller's action. However when I call the http://localhost:41083/Contact/CreateAccount page which is the same page without url rewriting it shows the page without any problem.
So is it caching the page by url? If so, how can I disable it? I am not using any output caching on the action and my site runs on IIS Express.
My Route Config and ContactController:
routes.MapRoute(
name: "musterimiz-olun",
url: "iletisim/musterimiz-olun",
defaults: new { controller = "Contact", action = "CreateAccount" },
namespaces: new[] { "OyakCorporate.Controllers" }
);
public ActionResult CreateAccount()
{
return View();
}
Using Redirect() instead of RedirectPermanent() in Application_Error() method on Global.asax solved the issue.

route controller action to just controller

I'm new into routing, and have a hopefully simple question toward it.
Right now, with the default routing, I need to use {controller}/{action}/{variable}.
This will mean (if I want to use a {variable}), I have to enter my URL as /Home/Index/1
Is there a way to make routing only use /Home/1 and send it to the Index action?
I need to use this only for a specific page, not all of them.
I have tried the following with no success:
routes.MapRoute(
"Alert",
"Alert/{id}",
new
{
controller = "Alert", action = "Index", id = ""
}
What you have should work. I surmise the reason your attempt does not work is that you have defined the default {controller}/{action}/{id} route before this one.
You have to register this route before any more general ones as the routing engine sends the request to the first action which matches the requested URL.

Categories

Resources