Vishal Kiri
Vishal Kiri

Reputation: 101

How to create custom Routing in ASP.NET Core

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}

Upvotes: 3

Views: 12685

Answers (4)

Samif
Samif

Reputation: 107

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

Upvotes: 0

Harsh Raj
Harsh Raj

Reputation: 89

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...
 }

Upvotes: 3

Rico Herlt
Rico Herlt

Reputation: 136

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

Upvotes: 1

Vishal Kiri
Vishal Kiri

Reputation: 101

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.

Upvotes: 5

Related Questions