asp.net-mvc 如何在操作链接中指定区域名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2036305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify an area name in an action link?
提问by Jeremy
I have a shared master page which I am using from 2 different areas in my mvc 2 app. The master page has an action link which currently specifies the controller and action, but of course the link doesn't work if I'm in the wrong area. I see no overload for actionlink that takes an area parameter, is it possible to do?
我有一个共享母版页,我在 mvc 2 应用程序的 2 个不同区域中使用它。母版页有一个当前指定控制器和操作的操作链接,但是如果我在错误的区域中,该链接当然不起作用。我看不到带区域参数的 actionlink 的过载,可以这样做吗?
回答by Jeremy
Figured it out..
弄清楚了..
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})
回答by Frank Schwieterman
Something I ran into right after this, that I imagine others might run into: If you need to link from within an area to an action not in an area, you still need to specify the Area as empty string.
在此之后我遇到了一些事情,我想其他人可能会遇到:如果您需要从区域内链接到不在区域内的操作,您仍然需要将区域指定为空字符串。
For instance, I moved some MVC code into an area, and found I needed to update urls in the master page that referenced other pages on the site.
例如,我将一些 MVC 代码移动到一个区域中,发现我需要更新引用站点上其他页面的母版页中的 url。
To specify an url to something not in an area, use
要指定不在区域内的东西的网址,请使用
Html.ActionLink("home", "Index", new { area = "", controller = "Home" })
回答by Subhash Rao
Use:
用:
Html.ActionLink("Text", "ActionName", "ControllerName", new { Area = "AreaName" }, null)
Note:4th parameter is to pass route Values, if you pass an empty parameter it will consider root structure and if you pass appropriate value it use it as area.
注意:第四个参数是传递 route Values,如果你传递一个空参数,它将考虑根结构,如果你传递适当的值,它会将其用作区域。
Also do not forget to use nullor new{}as the 5th parameter because passing nullor new {}while creating action link will not overload method for (text,action,controller,route data)or its (text,action,controller,route data,html attribute)so use the proper method
也不要忘记使用null或new{}作为,因为经过第5个参数null或new {}在创建跳转链接不会过载方法(text,action,controller,route data)或其(text,action,controller,route data,html attribute)所以使用正确的方法
回答by Sreejith
In MVC2 giving area="root"worked for me as below
在 MVC2 中给area="root"我工作如下
Html.ActionLink("Home", "Index", "Home", new { Area = "root" }, new{})
回答by dav_i
A neat trick you can do if you are using an area a lot in a View is define it as a variable at the top:
如果您在视图中大量使用某个区域,您可以做的一个巧妙的技巧是将其定义为顶部的变量:
@{ var awesomeArea = new { area = "Awesome" }; }
@Html.Action("Something", "Somewhere", awesomeArea)
@Html.ActionLink("Stuff", "FooBar", awesomeArea)
回答by Anthony Hart
Here is what I came up with as a solution to allow a user to link to the pre-built authentication systems.
这是我想出的解决方案,允许用户链接到预先构建的身份验证系统。
Each of my areas has a version of the _LoginPartial.cshtml file.
我的每个区域都有一个 _LoginPartial.cshtml 文件版本。
I probably could get the application to use a single version of the file, however I kept running into errors when trying to use a single login partial.
我可能可以让应用程序使用文件的单个版本,但是在尝试使用单个登录部分时我一直遇到错误。
It is only a slight modification to the original generated loginpartial, but it seems to work well when used in specific areas.
它只是对原始生成的 loginpartial 稍作修改,但在特定领域使用时似乎效果很好。
Here is the code that gets used in all of them:
这是在所有这些中使用的代码:
@if (Request.IsAuthenticated)
{
<text>
Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", new { area = "" }, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
}
else
{
<ul>
<li>@Html.ActionLink("Register", "Register", "Account", new { area = "" }, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", new { area = "" }, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
回答by ErdincDonmez
If you can not use standart web aplication link like About, Home, Contac etc from area. You change lines
如果您不能使用来自区域的标准网络应用程序链接,如关于、主页、联系等。你换线
@Html.ActionLink("Ana Sayfa", "Index", "Home", new { area = "" }, new {})
from
从
Views\Shared_Layout.cshtml
视图\Shared_Layout.cshtml
回答by Moleius
In my ASP Net Core app, I simply add the area to the html attributes like so:
在我的 ASP Net Core 应用程序中,我只是将区域添加到 html 属性中,如下所示:
@Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" , id = @Model.ID, name = @Model.name })
回答by Rob P
Using
使用
@Html.ActionLink("DisplayText", "ActionName", "ControllerName", new { area = "AreaName"}, null)
Will achieve what you are after.
将实现你所追求的。
The DisplayText is what will display (The same as <a href="#">DisplayText</a>), the ActionName is the method being called in the controller and ControllerName is obviously the controller you want to call! The next parameter is 'Route Value' where you would add your area. If you are currently in an Area and want to navigate back to your root Home/Indexfor example, you would leave the value as an empty string, eg new { area = ""}. The final value is 'Html Attributes' and where you would add a class if you wished and should be a nullif dont have any attributes to add. But as the last parameter is seen as 'Html Attributes'; in order for Route Values to be recognised, this should be null
DisplayText 是将要显示的内容(与 相同<a href="#">DisplayText</a>),ActionName 是控制器中正在调用的方法,ControllerName 显然是您要调用的控制器!下一个参数是“路线值”,您可以在其中添加您的区域。例如,如果您当前在一个区域中并且想要导航回您的根目录Home/Index,则可以将该值保留为空字符串,例如new { area = ""}. 最终值是“Html Attributes”,如果您愿意,您可以在其中添加一个类,并且null如果没有要添加的任何属性,则应该是一个。但由于最后一个参数被视为“Html 属性”;为了识别路由值,这应该是null

