How to create custom Routing in ASP.NET Core

空扰寡人 提交于 2020-01-06 02:33:26

问题


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}


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/37683121/how-to-create-custom-routing-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!