3

I need help with URL rewriting. I am new to ASP.NET Core MVC. When I type anything in {param} part then routing should redirect it to my controller.

So if anyone to types in {param} like

https://mydoamin.com/{param}

then it should be redirected to this url:

https://mydoamin.com/{controller}/{action}/{actionurl}={param}

4 Answers 4

5

I found the answer for my question. Just define new custom route in your startup.cs file before your default route.

routes.MapRoute(
    "Member",                                             // Route name
    "{actionURL}",                                        // URL with parameters
        new { controller = "Pages", action = "Details" }  // Parameter defaults
);

It's working form me.

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

Comments

3

We can do the same using this approach. I find this more convenient.

 [Http("add/user/{user}/{password}")]
 public IActionResult AddUser(string user, string password)
 {
      //do your things here...
 }

Comments

1

I could recommend you to see this blog post from Stephen Walther: ASP.NET 5 Deep Dive: Routing

I am not sure if this works as you want. https://mydoamin.com/{controller}/{action}/{actionurl}={param} seems not to be a valid URL to me. The part {actionurl}={param} is probably the query part which is comes as a key/value pair and starts always with a ?. You could probably fix your routing if your desired URL would look like https://mydoamin.com/{controller}/{action}/?key1=value1&key2=value2

Comments

0

I don't know still this is useful OR not but in asp.net core 3 this worked for me like a charm.

routes.MapRoute(
    "MoviesByReleaseDate",
    "movies/released/{year}/{month}",
    new { controller = "Movies", action = "ByReleaseDate" }
);

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.