Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action produces querystring, any way to produce full url?

Currently in my project I am using @Url.Action to create links in the href attribute of anchor tags.

for instance:

<a href='@Url.Action("Index", "Home", new { id = 10 })' id="btn">Click</a>

And this works fine, however the URL produced is...

/home/index?id=10

is there a way to return the url

/home/index/10

for SEO and aesthetic purposes? I used an id and number as a placeholder - in the real application it can and will use a string instead (so...

<a href='@Url.Action("Index", "Home", new { name="test" })' id="btn">Click</a>

to return the url

/home/index/test
like image 291
Cameron W. Avatar asked Oct 21 '22 06:10

Cameron W.


1 Answers

You need to ensure that the routing is correct. Make sure you have a route like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

For your 'real application', as you are using a parameter called name you will need one looking like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{name}",
    defaults: new { controller = "Home", action = "Index" });
like image 106
DavidG Avatar answered Nov 05 '22 08:11

DavidG