3

I'm trying to create well looking urls in my application, but when I'm using following construction

@Url.Action("Edit", "Account", new { userId = user.Id })

I've got next reference mysite.com/Account/Edit?userId=42.

How to get an url which looks like mysite.com/Account/Edit/42?

1
  • 7
    change userId to id then it'll hit your default route of {controller}/{action}/{id} Commented Dec 19, 2016 at 14:47

2 Answers 2

4

In your project under the App_Start folder you'll have a file named RouteConfig.cs this is where you can specify custom routing for your application.

By default you will have the following

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

Which will be hit if you did something like Url.Action("Edit", "Account", new {id = user.Id})

If you want to accomodate for your rule where you don't want to change userId to id then you can create one as follows:

  routes.MapRoute(
            name: "EditRule",
            url: "{controller}/{action}/{userId}",
            defaults: new { controller = "Edit", action = "Account", userId = UrlParameter.Optional }
        );

NOTE: the custom rule should appear before your default rule otherwise the default rule will be hit

Sign up to request clarification or add additional context in comments.

Comments

1

Use

@Url.Action("Edit", "Account", new { Id = user.Id })

and also change userId to Id if you have used it in your **controller method** and **model logic**

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.