简体   繁体   English

将博客的路由字符串 url 从 controller/action/{*Url} 更改为 {*Url}

[英]Change route string url of blog from controller/action/{*Url} to {*Url}

I have my personal blog site which was done using Asp.Net MVC and C# language.我有我的个人博客网站,它是使用 Asp.Net MVC 和 C# 语言完成的。

As I have configured RouteConfig with "controller/action/{*Url}" for blog URL where {*Url} could be any text.因为我已经为博客 URL 配置了带有“控制器/操作/{*Url}”的 RouteConfig,其中 {*Url} 可以是任何文本。

Now I want to change the Url pattern from "controller/action/{*Url}" to {*Url}.现在我想将 Url 模式从“控制器/动作/{*Url}”更改为 {*Url}。

Example:例子:

From below Url从下面 Url

https://abcd.co.in/home/newsdetail/abcd-xyz-opqr-stu https://abcd.co.in/home/newsdetail/abcd-xyz-opqr-stu

To

https://abcd.co.in/abcd-xyz-opqr-stu https://abcd.co.in/abcd-xyz-opqr-stu

I have done the following changes我做了以下更改

routes.MapRoute("", "mycontroller/myactionmethod/{*Url}", new { controller = "Home", action = "NewsDetail", Url = "" });

But it is calling every time URL hits my domain.但每次 URL 访问我的域时,它都会调用。

Please suggest what are the possible solutions that will not affect other functionality or URLs and it should fulfill my requirements.请建议哪些可能的解决方案不会影响其他功能或 URL,它应该满足我的要求。

Thanks in advance.提前致谢。

After a few google search and R&D on Attribute-based routing I have come up with one solution:在对基于属性的路由进行了几次谷歌搜索和研发之后,我想出了一个解决方案:

Steps:脚步:

  1. Need to define that I am using Attribute routing需要定义我正在使用属性路由

    routes.MapMvcAttributeRoutes();
  2. Remove existing route entry for the Action method.删除 Action 方法的现有路由条目。

  3. Set Attribute route entry path above the Action method.在 Action 方法上方设置 Attribute 路由入口路径。

    [Route("{Url}")] [路线(“{Url}”)]

  4. Just update the URL in href from previous to just URL.只需将 href 中的 URL 从之前的更新为 URL。

From

<a href="/home/newsdetail/@item.Url"> 

To

<a href="/@item.Url">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM