Asp.net mvc 3 在MVC3路由中使用Html锚
我的一个观点中有一个锚:Asp.net mvc 3 在MVC3路由中使用Html锚,asp.net-mvc-3,asp.net-mvc-routing,Asp.net Mvc 3,Asp.net Mvc Routing,我的一个观点中有一个锚: <a name="@("Doc" + Model.Key)"></a> 如何让MVC忽略url中的html锚(#Doc1)呢?我有一个路由可以这样做,但在硬盘故障时丢失了它,从内存中您需要指定一个包含 {controller}/{action}#{documentid} 其中documentid可以是任何不是id的东西 您还需要使控制器期望int?根据@BrettSmith发布的建议,我提出了以下解决方案: 我添加了第二条带有锚点和约束的路线
<a name="@("Doc" + Model.Key)"></a>
如何让MVC忽略url中的html锚(
#Doc1
)呢?我有一个路由可以这样做,但在硬盘故障时丢失了它,从内存中您需要指定一个包含
{controller}/{action}#{documentid}
其中documentid可以是任何不是id的东西
您还需要使控制器期望int?根据@BrettSmith发布的建议,我提出了以下解决方案:
routes.MapRoute(
"DocAnchor", // Route name
"{controller}/{action}/{id}#{anchor}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, anchor = UrlParameter.Optional }, // Parameter defaults
new { anchor = ".+" } // ".+" regex means any character one or more times.
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
<a href="@(Url.Action("Update", new { id=Model.DocumentId, anchor="Doc" + Model.DocumentId }).Replace("%23", "#"))">Go To Document Properties</a>
请注意,我正在传递锚点作为route数据对象的一部分(我还有一个错误,没有传递id,我也修复了这个错误)Url.Action
Url对字符串进行编码,因此我必须用哈希字符替换%23
,以使浏览器能够识别锚点routes.MapRoute(
"DocAnchor", // Route name
"{controller}/{action}/{id}#{anchor}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, anchor = UrlParameter.Optional }, // Parameter defaults
new { anchor = ".+" } // ".+" regex means any character one or more times.
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
<a href="@(Url.Action("Update", new { id=Model.DocumentId, anchor="Doc" + Model.DocumentId }).Replace("%23", "#"))">Go To Document Properties</a>