1

I'd like to change my routes so that instead of having:

/Users/Edit?UserID=1

I can do

/Users/Edit/1

How can I create a custom route to do that?

Also, can someone direct me to a good tutorial on routes? I don't wanna create a post every time I have a simple problem with routes.

3 Answers 3

2

Actually http://localhost/Users/Edit/1 can do the what you want without defining a new route. The default route structure is http://localhost/controllerName/actionMethodName/id.

So if you just use "id" instead of "UserID" in your action method. The number in the URL would be assigned to the parameter on action.

public ActionResult Edit(int id)

Here's a few lesson on routing. Take a look at them. It's a quite easy thing to understand.

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

Comments

0

You will want to use the htaccess file. You put in a regular expression which will extract pieces of the file path as if it is part of the query string.

This is a link I googled it may do the trick.

2 Comments

Since he is using ASP.NET MVC, he is probably not using Apache and doesn't have htaccess files.
Yeah, something similar though... IIS has to have a similar type file... I just don't know what it is called. Googling htaccess .net is bound to give him the right answers.
0

You will want to do something like this:

        routes.MapRoute(
            "MyRoute",
            "{controller}/{action}/{UserId}",
            new { controller = "DefaultController", 
                                  action = "DefaultAction", 
                                  UserId = UrlParameter.Optional }
        );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.