Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}

like image 778
Vishal Kiri Avatar asked Jan 26 '26 07:01

Vishal Kiri


2 Answers

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.

like image 178
Vishal Kiri Avatar answered Jan 29 '26 13:01

Vishal Kiri


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...
 }
like image 36
Harsh Raj Avatar answered Jan 29 '26 11:01

Harsh Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!