In ASP.NET MVC5, how can I hide the Action name when an a tag is generated in Razor using Url.Action? - asp.net

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.

Related

mvc3 map routes

I am looking to use the following urls and maps some routes accordingly
<domain>/Home/About
<domain>/Home/SiteList
<domain>/Site/<id>/ (this one is defaulted to the details view)
<domain>/Site/<id>/section1/ (this one goes to section1 route in the Site controller)
<domain>/Site/<id>/section2/ (this one goes to section2 route in the Site controller)
e.g.
<domain>/Site/london/siteinfo
The above are covered by
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
However, i also want to use the following routes
<domain>/Site/<siteid>/section1/controller/action (where controller gets data linked to the <siteid> and action is optional)
an example link would be:
<domain>/Site/london/siteinfo/manager (manager is a controller and will list managers for the site)
if have tried various routes and read various posts but could not find anything which was applicable to me.
Can anyone provide some help please?
Thanks
Rudy
Add one more route below "Default":
routes.MapRoute(
"MyRouteName", // Route name
"{controller}/{action}/{id}/{section}/{subsection}", // URL with parameters
new { controller = "Home", action = "Index", id= "", section= "", subsection = "" } // Parameter defaults
);

ASP.NET MVC How can better filter my route?

I have added a new route to my routing table:
routes.MapRoute(
"ModuleRoute", // Route name
"Module/{href}", // URL with parameters
new { controller = "Module", action = "GetHtml" }// Parameter defaults
);
I need this route to match on the following url structure:
/module/123abc.html
The problem is it also matches on this structure
/module/Launch/123abc.html
Calling link :
<%: Html.ActionLink("Launch", "Launch", new { href = item.Href })%>
How do I stop that from happening? I want he second structure to continue to be matched by the default route. I thought that because the number of parameters are different that this would not be a problem.
How can better filter my route to prevent this?
Thanks!
i agree with Max Toro, i've done some testing and that URL doesn't match Module/{href}.
This:
<%: Html.ActionLink("Launch", "Launch", new { href = item.Href })%>
is actually hitting the default route. You see this if you change the default route to the below (note the id is changed to href
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{href}", // URL with parameters
new { controller = "Home", action = "Index", href = UrlParameter.Optional } // Parameter defaults
);
so, this proves it is falling through the first route since it gets a proper url (no querystrings)
what it is doing (when you have the usual default route with id) is that the controller and action are matched but the id isn't. This is OK - the route still matches but leaves off the id. All additional values, such as your href are appended as querystring parameters so you end up with:
module/Launch?href=123abc.html
The way to get around it is to add another route similar to the one above that uses href instead of id
something like:
routes.MapRoute(
"Launch",
"Module/Launch/{href}",
new { controller = "Module", action = "Launch", href = UrlParameter.Optional }
);

ASP.NET - ActionResult parameter is coming back null always when passing string - why?

I am barely starting out with my first project on the ASP.NET MVC project type and I am creating a Details page where instead of passing the templated (int id), I would like to pass a string instead. But when I am in debug mode, and enter this in the URL, "myString" is null. Why so? Do I have to change anything else somehwere else?
So if I go to the URL and enter this:
http://localhost:2345/Bank/EmployeeDetails/3d34xyz
public ActionResult EmployeeDetails(string myString) // myString is null
{
return View();
}
In you Global.asax.cs file, you will have the following route mapped by default:
routes.mapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
That means that an url like http://localhost:2345/Bank/EmployeeDetails/3d34xyz will go to the Bank controller, the EmployeeDetails action and pass the value 3d34xyz into a parameter named id. It is perfectly alright to pass a string, but in order to make it work you have two options:
1) Rename the variable to id in your action method.
public ActionResult EmployeeDetails(string id) { ... }
2) Add another route that matches whatever name you want for your string. Make sure to make it more specific than the default route, and to place it before the default route in the Global.asax.cs file.
routes.mapRoute(
"BankEmployeeDetails"
"Bank/EmployeeDetails/{myString}"
new { controller = "Bank", action = "EmployeeDetails", myString = UrlParameter.Optional });
This will pass a default value of null to myString if no value is passed in the url, but with the url you specified you will pass the value 3d34xyz.
Rename myString to id if you are using the default route table.
Assuming you haven't modified the default routes (In your Global.asax.cs):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The method is expecting it to be named "id".
Change the name of myString to id.
I had the same problem, you just need to provide the id in the page where you the hyperlink for Details(in the Bank page here)
#Html.ActionLink("Details", "Details", new { id=""})
This solved my problem hope it helps you.
This for MVC 5.
And when you have tryed all of them and still it comes back null then give a full restart for Visual Studio. That's what eventually helped for me.

How to hide controller name in Url?

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
);

ASP MVC Routing with > 1 parameter

I have the following route defined
routes.MapRoute(
"ItemName",
"{controller}/{action}/{projectName}/{name}",
new { controller = "Home", action = "Index", name = "", projectName = "" }
);
This route actually works, so if I type in the browser
/Milestone/Edit/Co-Driver/Feature complete
It correctly goes to the Milestone controller, the edit action and passes the values.
However, if I try and construct the link in the view with a url.action -
<%=Url.Action("Edit", "Milestone", new {name=m.name, projectName=m.Project.title})%>
I get the following url
Milestone/Edit?name=Feature complete&projectName=Co-Driver
It still works, but isn't very clean. Any ideas?
When constructing and matching routes in ASP.NET routing (which is what ASP.NET MVC uses), the first appropriate match is used, not the greediest, and order is important.
So if you have two routes:
"{controller}/{action}/{id}"
"{controller}/{action}/{projectName}/{name}"
in that given order, then the first one will be used. The extra values, in this case projectName and name, become query parameters.
In fact, since you've provided default values for {projectName} and {name}, it's fully in conflict with the default route. Here are your choices:
Remove the default route. Do this if you don't need the default route any more.
Move the longer route first, and make it more explicit so that it doesn't match the default route, such as:
routes.MapRoute(
"ItemName",
"Home/{action}/{projectName}/{name}",
new { controller = "Home", action = "Index", name = "", projectName = "" }
);
This way, any routes with controller == Home will match the first route, and any routes with controller != Home will match the second route.
Use RouteLinks instead of ActionLinks, specifically naming which route you want so that it makes the correct link without ambiguity.
Just to clear up, here is what I finally did to solve it, thanks to the answer from #Brad
<%=Html.RouteLink("Edit", "ItemName", new { projectName=m.Project.title, name=m.name, controller="Milestone", action="Edit"})%>
You can try
Html.RouteLink("Edit","ItemName", new {name=m.name, projectName=m.Project.title});

Resources