I'm a little confused on something here. I have a form, and the URL for that form is:
http://domain.com/Home/requestquote
When I click submit on a form and the method handling this form located in the form controller sends back a view, the URI looks like this:
http://domain.com/form/requestQuoteSubmit where requestQuoteSubmit is the method.
This occurs both when validation sends the page back for errors and when there is a successful form submit.
On success the view being sent back is Home/thanks and on error it should just send back Home/requestquote. Everything seems to work fine except for the fact that the URI is not what it is supposed to be. This causes everything else on the page to break because my links look like this:
#Url.RouteUrl(Model.CompanyPageDatabaseModel.companyCode, new { Controller = MVC.services.Name, Action = MVC.services.ActionNames.page })
So that companyCode value isn't being passed around properly and forming the links correctly. I'm not sure how or why the form method is sending back the correct page, but setting the URI to itself?
Here are my routes.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
null,
"{action}",
new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Index },
new { RootAction = new RootActionConstraint() } // Route Constraint
);
routes.MapRoute(
null, // Route name
"{controller}/{action}", // URL with parameters
new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Index }, // Parameter defaults
new { controller = "Home|contact|franchise|form|resources|services|technology|community|careers|carriers|about" }
);
routes.MapRoute(
null,
"{companyCode}/{action}",
new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Index },
new { RootAction = new RootActionConstraint() } // Route Constraint
);
routes.MapRoute(
"jax",
"{companyCode}/{controller}/{action}",
new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Index }
);
The last route value is an example of a franchisee route.
Also here is the form code in the view:
#using (Html.BeginForm("requestquote", "form", FormMethod.Post))
{ }
Any ideas?
Thanks.
Edit: Added in my routes.
There is a concept referred to as PRG, which stands for Post-Redirect-Get.
The basic idea is that you Post the form to your application. After processing the input, you then Redirect to a Get request at the correct URL, instead of serving the user a content response directly from the submission.
This is good for many reasons, but the underlying one is separation of concerns. (When you don't separate concerns weird stuff like all the links might breaking on your page tends to happen...)
So, the solution for you is to process the submission, and if invalid, store your Validation content in TempData and return a RedirectToRouteResult(controller:"Home",action:"RequestQuote")
Update: I found the original article I read to learn this concept. It's in terms of the original ASP.NET MVC release, but it should be mostly the same. Check out http://www.eworldui.net/blog/post/2008/05/ASPNET-MVC---Using-Post2c-Redirect2c-Get-Pattern.aspx
Related
As the title says.
I have a route set up and working fine, which provides a default action when none is specified. I just want to hide the action from the URL because it's unnecessary clutter.
Setting the "ActionName" parameter as null, or "", will just result in the current page's action being substituted instead - which doesn't work.
I'm open to using #Html.ActionLink() if that will get me what I need.
My route definition is
routes.MapRoute(
name: "MyBookRoute",
url: "Book/{id}",
defaults: new { controller = "Book", action = "Index" }
);
If all else fails, I suppose I can deal with writing out the hrefs manually, but this should not be a difficult thing for Razor to do.
Has anyone else come across this and knows what to do?
Base on your route definition, then either
#Url.Action("Index", "Book", new { id = 1 })
or
#Html.ActionLink("Your link text", "Index", "Book", new { id = 1 }, null)
will remove the action name from the generated url.
I've looked through the various questions already asked on this topic, and I've spent time trying to get it working how I would like it to, but I haven't had much luck so hopefully someone here can help me fill in the gaps.
With a new site I'm creating I wanted to try getting the URL structure to be more RESTful (I wanted to do it with my first MVC3 creations, but, time did not permit such experimenting). However, I don't want the different URLs to all point to the same action. I want different actions for each resource requested to keep the controller code concise and intuitive.
Here is an example of the URL structure I'm shooting for:
/Case //This currently works
/Case/123 //This currently works
/Case/123/Comment //This one does not work (404)
Here is how I currently have my routes setup:
routes.MapRoute(
"Case",
"Case/{id}",
new { controller = "Case", action = "Number" }
);
routes.MapRoute(
"CaseComment",
"Case/{caseId}/Comment/{id}",
new { controller = "Case", action = "CaseComment" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The first two URL's I listed are worked correctly with this route structure. The first URL takes me to my listing page. When an id is specified, I hit the Number action so I can show details for that particular record. The Comment URL is not working.
I have the action for the third URL defined as:
public ActionResult CaseComment(string caseId, string id) {
//Narr
}
What am I missing? And, could I set this up in an easier fashion for future resources?
I believe MapRoutes are order specfic, so
/Case/123/Comment
is using your
routes.MapRoute(
"Case",
"Case/{id}",
new { controller = "Case", action = "Number" });
route, thus throwing a 404. Most specific route should be place above more general routes.
Try placing the CaseComment route above the Case route.
Mapping routes are order specific.
One thing you may wish to consider for restful routing in MVC is a project called RestfulRouting that is on GitHub originally written by Steve Hodgekiss.
https://github.com/stevehodgkiss/restful-routing
If nothing else, looking at the code may well help you.
Hope this helps.
I needed to make the id parameter optional.
routes.MapRoute(
"Case Resources",
"Case/{caseId}/{action}/{id}",
new { controller = "Case", action = "CaseComment", id = UrlParameter.Optional }
);
In my MVC application I want to create a route such that when a user requests a URL starting with a prefix some specific action is invoked.
For example, I want a route that would map processData{whatever} onto an action so that when a user requests processData, processData.asmx or processDataZOMG or whatever else with processData prefix that action is invoked.
I tried the following route
routes.MapRoute(
#"ProcessData", #"processData*", //<<<< note asterisk
new { controller = #"Api", action = #"ProcessData" } );
but it doesn't match processData and anything with that prefix - route matching falls through and the request is redirected to the main page.
How do I make a route that matches all paths with a specific prefix onto a specific controller-action pair?
Try the following: Update: This solution does not work, please refer to the solution I offer in my comment to this answer.
routes.MapRoute(
#"ProcessData", #"processData/{*appendix}", //<<<< note asterisk
new { controller = #"Api", action = #"ProcessData" } );
You could use route constraints:
routes.MapRoute(
"ProcessData", // Route name
"{token}", // URL with parameters
new { controller = "Api", action = "ProcessData" }, // Parameter defaults
new { token = #"^processdata.*" } // constraints
);
I have been looking around for a solution for my problem. Found alot of similar issues, but none of them led to a solution for me.
I am trying to register an Area within an Area. This works however it "partially" screws up my routing.
My route registrations in the order they are registered, consider the FooBar and Foo registrations to be coming from AreaRegistrations
routes.MapRoute("FooBar_default",
"Foo/Bar/{controller}/{action}",
new { area = "Foo/Bar", controller = "Home", action = "Index"},
new[] { BarHomeControllerType.Namespace }
);
routes.MapRoute("Foo_default",
"Foo/{controller}/{action}/{id}",
new { area = "Foo", controller = "Start", action = "Index", id = UrlParameter.Optional },
new { controller = new NotSubArea()},
new[] { typeof(StartController).Namespace }
);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("PagesRoute", "Pages/{action}", new { controller = "Pages", Action "Index" }).DataTokens["UseNamespaceFallback"] = false;
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { typeof(HomeController).Namespace }
).DataTokens["UseNamespaceFallback"] = false;
Now the following problem occurs. When going to Website/Foo/ or Website/Foo/Bar links in those pages are generated correctly using:
!{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = "Foo/Bar"})}
or
!{ Url.Action("Index", "Home", new { area = "Foo/Bar"}) } //or a different area
However when i use this in my main pages, in other words Website/ or Website/Home etc..
!{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = ""})}
or
!{ Url.Action("Index", "Home", new { area = ""}) }
//or with no area identifier specified
It generates the Url: Website/Foo/Bar/Home etc... Which ofcourse is wrong.
When i remove the Area registration for Foo/Bar it all works again. Going to the urls Website/Home/About or Website/Home directly does display the right pages, so im guessing somehow the internal UrlHelper is picking the wrong routes to render.
I tried switching the order of the FooBar_default and Foo_Default routes, so that the Foo_default route is registered before the FooBar_default route, but then the area does not work anymore (resource not found) and the links are still generated incorrectly.
What i find most odd is that removing the Foo/Bar registration solves the problem. I was hoping someone could shed some insight on this matter..
What you need to understand that an Area is just a routing concept which Microsoft have neatly wrapped up the concept or UrlRouting to get people started.
You can actually get the MVC framework to route your request however you like according to your requirements.
What you might need to look at doing, is writing your own RouteHandler. This will enable you to correctly direct how the MVC framework routes any request accoring to your requirements.
See this answer to asp.net mvc complex routing for tree path as an example to get you started.
chris166 outlines that my implementing your own IRouteHandler, and mapping your route to use that instead should get you what you need. Its a bit more effort than using out the box solution of areas, but should get you better results.
routes.MapRoute(
"Tree",
"Tree/{*path}",
new { controller = "Tree", action = "Index" })
.RouteHandler = new TreeRouteHandler();
How to hide controller name in Url?
I use the ASP.NET MVC.
The original url is: http://www.sample.com/Users.mvc/UserDetail/9615
The "Users" is controller name, the "UserDetail" is action name, and the "9615" is UserId.
How can I hide the controller name and action name in the url.
Just like this: http://www.sample.com/9615
I have writed the following code in the Global.ascx.cs to hide the action name:
routes.MapRoute(
"UserDetail", // Route name
"Users.mvc/{UserId}", // URL with parameters
new { controller = "Users", action = "UserDetail", UserId = "" } // Parameter defaults
);
Using the above code I hid the action name and got this url: http://www.sample.com/Users.mvc/9615
But how can I hide the controller name and get this url: http://www.sample.com/9615
Thanks.
The idea is the same. You do just the thing you did to the action. However, your problem arises from the fact that IIS is probably not mapping www.xyz.com/1234 to ASP.NET runtime. To do so in IIS7, enable integrated mode and in IIS6, add a wildcard mapping in handler map that maps everything to ASP.NET.
To add a wildcard map, see http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx (Search for "IIS6 Extension-less URLs" in that page)
After that, simply add a route:
routes.MapRoute("UserDetails", "{UserID}/{*name}",
new { controller = "Users", action = "UserDetail" , UserID=""});
This should do the trick.
MVC recognizes the difference between "{UserID}" and "{id}" so if you are going to have a route with only "{UserID}" in the Url you need to place it first in the list other wise it never gets hit. And make sure the default includes "id" since it will continually loop over "UserDetails" unless the default references id as apposed to UserID. I found this format works for me:
routes.MapRoute("UserDetails",
"{UserID}",
new { controller = "Users", action = "UserDetail", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = "" } // Parameter defaults
);