MVC中的一个简单任务有时成为一个艰巨的挑战。
那我有一个叫做Admin的区域。我在该区域的Shared目录中有一个名为“Forbidden”的页面。
目标很简单:我需要创建一个Html.ActionLink,生成一个链接,返回到管理区域外的主页。
所以我尝试,<%= Html.ActionLink(“Back”,“Index”,new {controller =“Home”})%>,及其生成:
http://localhost/Admin/Home/Index
它的错!我想要:
http://localhost/Home/Index
解决方法
尝试这个 :
<%= Html.ActionLink("Back","Index","Home",new { area = "" },null) %>
当使用区域时,您应该总是通过添加如上所述的路径值来指定您的ActionLink中调用的区域。如果链接位于区域之外(如您所见),则只需使用该区域的空参数即可。
有一个很好的扩展,我发现任何ASP.NET MVC项目(T4MVC)必不可少。它使您的ActionLinks看起来更清洁,并保护它们免受错误。
所以上面的代码看起来像这样:
<%= Html.ActionLink("Back",MVC.Home.Index()) %>
使用区域时:
<%= Html.ActionLink("Some Link",MVC.Admin.someController.someAction()) %>
它是Codeplex here的MvcContrib项目的一部分
你应该考虑使用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。