1

I have Login Controller and Register ActionResult

   public ActionResult Register()
        {
            return View();
        }

If i post any data to Register actionresult url seems like below,

WebSiteName/Login/Register

I want to change route url as WebSiteName/Login/Reg=Id?

So i tried below however i could not change route url.

   routes.MapRoute(
         name: "something",
         url: "Login/Reg=Id?",
         defaults: new
         {
             controller = "Login",
             action = "Register",
             id = id = UrlParameter.Optional
         }
     );

So how can i change url in asp.net mvc ?

Any help will be appreciated.

Thanks.

1
  • Why do you want your URL to look like that? Commented Jan 24, 2014 at 16:42

2 Answers 2

6

You are trying to use incorrect form of a url parameter. The options you have are:

  1. Url part parameter: WebSiteName/Login/Reg/{id}

    For this you can use following config

    routes.MapRoute(
         name: "something",
         url: "Login/Reg/{id}",
         defaults: new
         {
             controller = "Login",
             action = "Register",
             id = UrlParameter.Optional
         }
     );
    
  2. Query string parameter: WebSiteName/Login/Reg?id={id}

    Here you do not need to specify the parameter in config at all:

    routes.MapRoute(
         name: "something",
         url: "Login/Reg",
         defaults: new
         {
             controller = "Login",
             action = "Register"
         }
     );
    

Of course in both cases it is assumed your action Register has parameter id.

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

3 Comments

I wrote your code before"Default" Controller Action etc , it worked now.If i wrote your code after "Default" it does not work , i don't know why.Thanks for your help.
@user3230063, that can be easily explained. When new request is received, ASP.NET MVC tries to find corresponding route in the routes table, and takes the first match. Default route matches almost everything, so whatever is added to the table after it not likely to be reached. That is why you should add all specific routes before the default one.
Please help me for this question . stackoverflow.com/questions/21335361/…
0

Here I've remove last two passing parameter from the URL

this is my url link -> http://localhost:12345/User?value=98998?id=2

and I want to remove value and id parameter from the url link

Step-I

Modify your Routeconfig.cs like this

routes.MapRoute(
   "User",
   "User/{value}/{id}",
   new { controller = "User", action = "Index", value= UrlParameter.Optional, id = UrlParameter.Optional }
);

Step-II

User Controller

public ActionResult Index(string value, int id)
{
    // write Your logic here 
    return view();
}

Step-III

Create an Index view for UserController

@Html.ActionLink("LinkName", "Index", "User", new {value = "98998", id = "2"},null)

Finally we got a result: http://localhost:12345/User/98998/2

Likewise we can remove multiple parameter from the url

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.