wh-dev
wh-dev

Reputation: 547

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

Upvotes: 5

Views: 2305

Answers (1)

DavidG
DavidG

Reputation: 119186

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

Upvotes: 3

Related Questions