I have a MVC3 app with several routes. Two of them are defined like this:
routes.MapRoute(
null,
"System/{name}", // URL with parameters
new { controller = "Systems", action = "Index" } // Parameter defaults
);
routes.MapRoute(
null,
"Carrier/{name}", // URL with parameters
new { controller = "Carriers", action = "Index" } // Parameter defaults
);
Now, in my menu I have two links to these routes that are created using Url.Action:
Url.Action("Index","Systems")
Url.Action("Index","Carriers")
Now when i start the application, everything seems fine, and the links in the menu show as /System/ and /Carrier/, and that is the expected value.
However, when i browse to for example /System/MySystem in the web page i would still want the links to point to the same place, but now they point to /System/MySystem and /Carrier/MySystem.
I have tried lots of things to keep the link from using the name from the route value, but to no avail. The strangest case i experienced was when i tried this:
Url.Action("Index","Systems", new{name = (string)null})
Now the link showed up as
/System?name=MySystem
Are there any good way here to make sure that the name value from the route values does not interfer with these links in any way?
As you noticed the Url. helpers reuse the previously given route parameters.
As a workaround (I hope that there is a more elegant solution...) you can remove the name entry from the RouteData.Values in your view:
So before calling you Url.Action in your view:
Url.Action("Index","Systems")
Url.Action("Index","Carriers")
Remove the prefilled name from the RequestContext:
#{
Request.RequestContext.RouteData.Values.Remove("name");
}
It's also an workaround but if you slightly modify your routes with providing a default null value for your name segment:
routes.MapRoute(
null,
"System/{name}", // URL with parameters
new { controller = "Systems", action = "Index", name = (string)null }
);
routes.MapRoute(
null,
"Carrier/{name}", // URL with parameters
new { controller = "Carriers", action = "Index", name = (string)null }
);
Your original solution ("nulling" the name in the Url.Action) will also work :
#Url.Action("Index", "Systems" , new {name = (string)null} )
Related
I'm trying to create a new Route in MVC3 to achieve the link http://localhost/Product/1/abcxyz:
routes.MapRoute(
"ProductIndex", // Route name
"{controller}/{id}/{name}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
And I used Route Link like this:
<li>#Html.RouteLink("My Link", "ProductIndex", new { controller = "Product", id = 10, name = "abcxyz" })</li>
Product Index action:
public ViewResult Index(int id, string name)
{
var product = db.Product.Include(t => t.SubCategory).Where(s => s.SubID == id);
return View(product.ToList());
}
The url render as I expected. But when I click on it, I got a 404 error with message
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
UPDATE
I place that Route above Default Route and the URL work fine. But there's a problem occure. My index page http://locahost point directly to Index action of Product controller, but I want it points to Index action of Home controller instead
It's because you have 2 optional parameters in your route and the engine can't work out which one to set the value to. See my answer to a similar issue here
You can create a specific route for your products controller first (with mandatory id) and then have the generic fallback route afterwards.
routes.MapRoute(
"ProductIndex", // Route name
"products/{id}/{name}", // URL with parameters
new { controller = "Product", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
Try it
routes.MapRoute(
"Default", // Route name
"{controller}/{id}/{name}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
for routing details see this link. In this link every type of routing is discussed.
http://www.codeproject.com/Articles/408227/Routing-in-MVC3
I have two methods on my controller
Function FollowUp(id As String) As ActionResult
and
Function Details(id As String) As ActionResult
When I write <%:Html.ActionLink("FollowUp", "FollowUp")%> the generated link does not include the id (I get /Project/FollowUp)
But when I write <%:Html.ActionLink("Details", "Details")%> the generated link does include the id (I get /Project/Details/25)
Can anyone explain why this happens?
If you create a link for a routing that takes a parameter, if your current route data includes that variable, then the same value will be included in your URL. For example: if you had just the standard routing:
routes.MapRoute(
null,
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And were currently at /home/index/25, then if you make a new link #Html.ActionLink("test","testing") then it would include the current id: /home/testing/25.
There are two ways I can think of this default id won't show up:
1) You have a different routing for "FollowUp" which doesn't include the {id} parameter.
routes.MapRoute(
null,
"Project/FollowUp",
new { controller = "Project", action = "FollowUp" }
);
2) You're creating the "FollowUp" link on a page that doesn't have an id currently included in the routing. (like home/index instead of home/index/25)
I would like to create a member area on my site with the following URL patterns:
Pattern for logged out user:
domain.com
domain.com/About
domain.com/Blog
domain.com/Blog/1 (where 1 is the post ID)
But I also have a member area where I prefix the Url with Member like this:
domain.com/Member/MyProfile
domain.com/Member/MySettings
This seems simple, but I can't see an obvious way to make routing rules for this. I have:
routes.MapRoute(
"Member", // Route name
"Member/{controller}/{action}/{id}", // URL with parameters
new { controller = "Task", 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
);
This works great for the member when you are logged in, but unfortunately the first rule also matches the logged out view and Url.Action("Blog", "Home") produces a Url that looks like this:
domain.com/Member/Home/Blog
How do I tell Url.Action that it must form Urls with the default rule outside the member area?
You could use a real MVC area instead of trying to simulate one. There's also a video you might checkout. The idea is that you leave your default route definition in Global.asax and then add a Member area to your site which will have a separate route configuration.
I have added a controller to my project named UserManager (automatically generated from the ado.net framework)
When I start the application, attempts to navigate to http://server/UserManager/ are met with a 404 error, but if I go to http://server/UserManager/Index the action is found and executes properly.
Is this a case of the controller not being called or is it just not treating index as the default action. Where are these properties set?
UPDATE
It seems that the problem derived from the fact that the default route is set to
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Permits", action = "ListApplications", id = UrlParameter.Optional }
This conflicts with the naming scheme for Usermanager (where the default is Index)
I struggled with ohow to add alternate routes that provided for default actions, but eventually figured out that the order of route addition determines which route takes the request (the earlier the route is added, the more chances it has to meet the criteria.)
Thanks
You need to ensure that the default route mapping specifies "Index" as the default action in your global.asax file.
Check that you have the following setting in your global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
IN REPLY TO YOUR COMMENT:
Only by way of adding new route mappings. You also need to be aware that the first matching route mapping will be applied, so the order you specify the mappings in Global.asax is crucial.
For example, we wanted our FAQ controller to work with a URL http://domain/faq/{id} without the action specified in the URL, so we declared the following mapping before the default:
routes.MapRoute("Faq", "Faq/{id}", new { controller = "Faq", action = "Answer" });
In Global.asax.cs, check the default route is set up:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional}
);
Also, check that the controller is called UserManagerController, and derives from Controller
In my ASP.NET MVC3 project, I have a folder called Content (the default folder for an MVC project). But I also have a controller called Content. And when I want to use the default actions of this controller, I simply use http://domain/content/, which is equivalent to http://domain/content/index. But IIS returns 403 error and thinks that I'm gonna get the directory list of the Content Folder. Well, this question is already discussed in this question. But I don't know how to rewrite my URL to append the default action to it. May someone help please.
You can get around this by changing routing configuration to specify:
routes.RouteExistingFiles = true;
You will then need to set up some ignore rules to prevent genuine static content being gobbled up by the routing engine.
For example, I have a folder called Touch in my app, and I also have a specific route for Touch. So the working config is:
routes.RouteExistingFiles = true;
routes.IgnoreRoute("Touch/Client/{*touchclientversion}", new { touchclientversion = #"(\d*)(/*)" });
I agree that this kind of thing should generally be avoided, but sometimes it's nice to have pretty URLs :-)
You can add a default route like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
In your case:
routes.MapRoute(
"DefaultContent", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Content", action = "Index", id = "" } // Parameter defaults
);