ASP.NET MVC ActionLink外部区域 [英] ASP.NET MVC ActionLink outside area

查看:67
本文介绍了ASP.NET MVC ActionLink外部区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC中完成一项简单的任务有时会成为一项艰巨的挑战.

A simple task in MVC, sometimes becomes a hard challenge.

好吧,我有一个名为Admin的区域.我在此区域的共享"目录中有一个名为禁止"的页面.

Well,i have an Area called Admin. I've a page named "Forbidden" inside the Shared's directory in this area.

目标很简单:我需要创建一个Html.ActionLink来生成一个链接,以返回到首页,该页面位于 OUTSIDE 管理员区域.

The goal is simple: I need to create an Html.ActionLink that generates a link to return to Home page which is OUTSIDE the Admin area.

所以我尝试<%= Html.ActionLink("Back","Index",new {controller="Home"})%>及其生成:

http://localhost/Admin/Home/Index

错了!我要

http://localhost/Home/Index

如何创建从区域到默认控制器结构的链接?

How can i create a link from an area to the default controllers structure?

推荐答案

尝试一下:

<%= Html.ActionLink("Back", "Index", "Home", new { area = "" }, null) %> 

使用Areas时,应始终通过添加上述路由值来指定要在ActionLinks中调用的区域.如果链接位于该区域之外(如您的情况),则只需为该区域使用一个空参数即可.

When using Areas, you should always specify the area your are calling in your ActionLinks by adding a route value as above, If the link is outside the area (as in your case), just use an empty parameter for the area.

有一个不错的扩展,我发现它在任何ASP.NET MVC项目( T4MVC )中都是必不可少的.这样可以使您的ActionLink看起来更加整洁,并保护它们免受错误的侵害.

There's a nice extension that i find essential in any ASP.NET MVC project (T4MVC). It makes your ActionLinks look much cleaner and it protects them against errors.

所以上面的代码看起来像这样:

So the above code will look something like this :

<%= Html.ActionLink("Back", MVC.Home.Index()) %>

以及在使用区域时:

<%= Html.ActionLink("Some Link", MVC.Admin.SomeController.SomeAction()) %>

这是Codeplex 上的 MvcContrib 项目的一部分.这里

It's a part of the MvcContrib project on codeplex here

您应该考虑使用它.

这篇关于ASP.NET MVC ActionLink外部区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆