Routing with a Guid in ASP.NET MVC - asp.net

I have a url which will take the following form:
/AuditReview/Review/15d49a66-5c11-492c-921f-9e1700bd2618
I cannot get this to route, my routes look like this:
MvcRoute.MappUrl("{controller}.mvc.aspx/{action}/{auditEventUid}")
.WithDefaults(new {controller = "AuditReview", action = "Review"})
.WithConstraints(new { controller = "AuditReview", action ="Review", auditEventUid = new GuidConstraint() });
MvcRoute.MappUrl("admin/{controller}.mvc.aspx/{action}")
.WithDefaults(new { controller = "audit", action = "index" })
.WithConstraints(new{controller = "audit"})
.AddWithName("admin", routes);
MvcRoute.MappUrl("{controller}.mvc.aspx/{action}")
.WithDefaults(new {action = "Index"})
.AddWithName("Default", routes);
Can anyone suggest a route to make this work?

Your current routing rules would require you to access the url using:
/AuditReview.mvc.aspx/Review/15d49a66-5c11-492c-921f-9e1700bd2618
If you're running IIS 7 or have enough access to the server to install wildcard mapping you can just remove the .mvc.aspx from your routing rules and your original url will work.

This should work - I am able to map a simpler scenario fine. Can you get this scenario to work with a simpler route (e.g. the default "{controller}.mvc/{action}/{id}" route)?
Have you tried removing the GuidConstraint or testing to make sure that the GuidConstraint works? Perhaps that is not working properly.

Related

MVC controller action causing 404 not found error

NOTE: This is not a duplicate of another question as my first page works fine, it is other pages/actions that are not working.
I've ready many posts so far and nothing comes close. This works just fine when run locally on my development box. After I copy to the server, I see the problem. My default route works fine, I get the expected index page.
public ActionResult Index()
{
return View();
}
My RouteConfig contains:
routes.MapRoute(
name: "AnnualFees",
url: "{controller}/{action}/{id}",
defaults: new { controller = "AnnualFees", action = "Index", id = UrlParameter.Optional }
);
Problems arise when I want to reach anything other than Index. For example, this action causes a 404 not found error:
public ActionResult renderForm()
{
return PartialView("_formPanel");
}
Again, works as it should on my local dev box. But on the server I get Requested URL: /AnnualFees/renderForm 404 error The resource cannot be found.
UPDATE
Ok, doing some more research and trial and error, I discovered something and have a bit more to add.
This app is running under a current website in IIS, where I created a Virtual Folder/application under the website root. If I navigate to www.mysite.com/AnnualFees I get the first page of my MVC app as expected. However, the only way I can get to any other action in my AnnualFeesController, I have to double up the controller name like www.mysite.com/AnnualFees/AnnualFees/renderForm which works but is ugly and not quite right. How can I get rid of the redundancy?
So the problem, as I noted in the comment, is that you have a folder, and under it goes the route. If you do not provide any parts of the route, just calling
www.mysite.com/AnnualFees
this uses all defaults as specified in your route config, and you get the default page. However if you type
www.mysite.com/AnnualFees/ActionName
MVC sees that as controller ActionName and no action no id provided. This does not exist in your app, thus the error.
The root problem here is that websites are not supposed to live under folders, they are supposed to live under top domains. That is, it is not expected that site root URL is www.mysite.com/AnnualFees, it is supposed to be just www.mysite.com. But even that would be fine if you did not have your main controller and IIS folder with the same names, producing unwanted duplication.
You can however change you route to make AnnualFees a default controller. Simply remove the controller part like so:
routes.MapRoute(
name: "AnnualFees",
url: "{action}/{id}",
defaults: new { controller = "AnnualFees", action = "Index", id = UrlParameter.Optional }
);
Now you should be able to use
www.mysite.com/AnnualFees/ActionName
Again, note that in the above URL "AnnualFees" is not a controller name, it is in fact no visible to MVC app at all.
There is however a caveat. Imagine you need to add another controller. Now the default and only route would not work with it. The key is to provide a separate route for this controller, with hardcoded first part
routes.MapRoute(
name: "NewControllerRoute",
url: "NewControllerName/{action}/{id}",
defaults: new { controller = "NewController", action = "Index", id = UrlParameter.Optional }
);
Make sure to put this route before the default one, so that all requests to this controller are routed correctly, and all other requests go to "AnnualFees".

Adding attribute route breaks config-based route (.NET MVC5)

So, we're updating a project from web forms to .NET MVC. To support other applications that deep link into our application, I'm trying to add attribute routes to the relevant controller actions that mimic the old web forms paths.
I have an event action on the Home controller. The configuration has a route for this to remove the controller name.
routes.MapRoute(
name: "eventdetails_nohome",
url: "event/{id}/{occurrenceid}",
defaults: new { Controller = "Home", action = "Event", occurrenceid = UrlParameter.Optional },
constraints: new { id = #"\d+", occurrenceid = #"\d+" }
);
That route works just fine for routes like http://myapp/event/123/456, and the default routing like http://myapp/home/event?id=123&occurrenceid=456 also works.
So far so good, but if I add this route attribute to the action:
[Route("~/ViewEvent.aspx")]
public ActionResult Event(int id, int occurrenceid)
Then the only route that works is http://myapp/ViewEvent.aspx?id=91918&occurrenceid=165045. The routes that worked before start returning
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /event/123/456
I've used the routedebugger extension, and I can verify that even with the attribute route, my old route is still the first to work. So why would I be getting "resource cannot be found" errors?
Note: as a workaround, I've found that I can just do a traditional route configuration like
routes.MapRoute(
name: "Legacy event",
url: "ViewEvent.aspx",
defaults: new { Controller = "Home", action = "Event" }
);
I'm still curious why the attribute route would break existing routes, though, as I thought you were supposed to be able to use both at the same time.
Take a look at Attribute Routing in ASP.NET MVC 5
Another article with the same heading
Attribute Routing in ASP.NET MVC 5
Attribute routes overrides the convention based route. If you use more than one URL for action, you can use multiple route attributes on the action...
[Route("event/{id:int}/{occurrenceid:int}")]
[Route("event")]
[Route("~/ViewEvent.aspx")]
public ActionResult Event(int id = 0, int occurrenceid = 0) {
return View();
}
The following URLs all routed to the above action.
http://myapp/event/123/456
http://myapp/home/event?id=123&occurrenceid=456
http://myapp/ViewEvent.aspx?id=91918&occurrenceid=165045

ASP.net MVC 5 Route with two parameter

I want create web application with 2 parameter. Add this Code to RegisterRoutes function:
routes.MapRoute(
"pageroute",
"page/{pageid}/{pagename}",
new { controller = "page", action = "Index", pageid = "", pagename = "" }
);
and add this method in pageController:
public ActionResult Index(int pageid,string pagename)
{
return View();
}
Now when i running application with this parameter
http://localhost:1196/page/4/Pagename
Application run successfully but when running with this parameter
http://localhost:1196/page/4/Pagename.html
Application return 404 error
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
while add .html in parameter return 404 error. why?
Because by default HTML files are not being served through MVC/IIS.
Your application looks for a physical file named Pagename.html and cannot find it there. Hence - 404.
To make it work, you need to setup your IIS to catch requests for files with HTML extension and pass this request to MVC.
EDIT: Here is a similar question where OP found a solution by switching "Managed Pipeline Mode" to "Classic".
Try changing the Route to:
routes.MapRoute(
"pageroute",
"page/{pageid}/{*pagename}",
new { controller = "page", action = "Index", pageid = "", pagename = "" }
);
This will then match everything as in page/1/* so the .html should go via this route.
I was unable to reproduce an issue where a .html file gave a 404 using a scenario similar to the question, so there may be some other routing issue or IIS configuration causing this.

MVC route conflicts with service route

I'm trying to convert the existing web project to MVC. I made a standard MVC project in VS 2012. It added routing configuration. My existing project already contained routing entry used by WCF services. So now routes are configured like this:
// Was here before, used by services
routes.Add(new ServiceRoute("AppServer", new WebServiceHostFactory(), typeof(MyService)));
// the rest is added by vs to configure a default controller
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I realized that service part should always go first because it is more restrictive. Service doesn't work if it is put to the end. But now the problem is that
#Html.ActionLink("text", "MyAction", "MyController")
now generates me the links of type
http://localhost/AppServer?action=MyAction&controller=My
instead of
http://localhost/My/MyAction
what I would expect.
Does anyone know how to make service route and mvc-related routes "live in peace" i.e. not to affect each other?
Try and put the MapRoute on top but add a fourth parameter for contraints, like this:
new { controller = "regex-for-!=-AppServer" }
This way, when creating a link the helper will use the first route found, but still incoming requests to "/AppServer" will be skipped and processed by the ServiceRoute.

Redirect to default full URL with mvc3

I want to give the users a friendly URL, with the desired area, and whenever they enter it, the site is forwarded to the default controller/action., but I am having a real hard time figuring out how to do it.
Example: someone types http://mySite.com/System and the routing engine redirects to the complete default url http://mySite.com/System/Auth/SignIn
I tried this, but it isn't working
routes.MapRoute(
"System", // Route name
"System/{controller}/{action}", // URL with parameters
new { area = "System", controller = "Auth",
action = "SignIn", id = UrlParameter.Optional } // Parameter defaults
);
PS: as I am using areas, System in this case is the {area}, Auth is the {controller} and SignIn is the {action}.
Could use a SystemController at the root level of your application, of which the Index() action would simply redirect to /System/Auth/SignIn.
is there any Reverse Proxy Solution is the architecture which host your application ?

Resources