AreaActionLink 扩展方法和命名空间错误?
我想向 HtmlHelper
类添加一个扩展方法,以便开发人员可以一般检索和 AreaActionLink
,而无需添加 new { area = "MyArea " }
每次都不必指定 Controller
。如果我指定命名空间或将区域控制器的命名空间放入 Web.config 中,这一切都会很好地工作。
例如,如果我将区域控制器命名空间更改为 My.Web.Controllers
而不是 My.Web.MyArea.Controllers
,它会抛出 404,但如果我使用它解析的命名空间适当地。
public static MvcHtmlString AreaActionLink<T>(this HtmlHelper helper, string linkText, string actionName, object routeValues, object htmlAttributes) where T : IController
{
RouteValueDictionary routes = new RouteValueDictionary(routeValues);
string area = typeof(T).GetArea();
if (!routes.ContainsKey("area"))
routes.Add("area", area);
return helper.ActionLink(linkText,
actionName,
typeof(T).Name.Replace("Controller", string.Empty),
routes,
htmlAttributes as Dictionary<string, object>);
}
如果在调用 AreaActionLink 时命名空间是完全限定的
namespace My.Web.Areas.MyArea.Controllers
{
[Area("MyArea")]
public class OtherPlaceController : Controller
{
//...
}
}
并按如下方式调用,则此方法有效:
<%=Html.AreaActionLink<OtherPlaceController>("Link Text", "MyAction")%>
但如果我尝试展平命名空间层次结构,这样我就不必为每个 Area 添加新的命名空间,它会抛出 404。
namespace My.Web.Controllers
{
[Area("MyArea")]
public class OtherPlaceController : Controller
{
//...
}
}
似乎 .Areas
命名空间的部分很重要,但我不明白为什么......
I wanted to add an extension method to the HtmlHelper
class so that developers could generically retrieve and AreaActionLink<T>
without having to add the new { area = "MyArea" }
each time as well as not have to specify a Controller
. This all works well and good if I specify the namespace or put the namespace of the Area Controller in the Web.config.
For instance if I change the Area controller namespace to My.Web.Controllers
rather than My.Web.MyArea.Controllers
it throws a 404 but if I use the namespace it resolves properly.
public static MvcHtmlString AreaActionLink<T>(this HtmlHelper helper, string linkText, string actionName, object routeValues, object htmlAttributes) where T : IController
{
RouteValueDictionary routes = new RouteValueDictionary(routeValues);
string area = typeof(T).GetArea();
if (!routes.ContainsKey("area"))
routes.Add("area", area);
return helper.ActionLink(linkText,
actionName,
typeof(T).Name.Replace("Controller", string.Empty),
routes,
htmlAttributes as Dictionary<string, object>);
}
This works if the namespace is fully qualified when calling AreaActionLink
namespace My.Web.Areas.MyArea.Controllers
{
[Area("MyArea")]
public class OtherPlaceController : Controller
{
//...
}
}
and called like this:
<%=Html.AreaActionLink<OtherPlaceController>("Link Text", "MyAction")%>
but if I try to flatten the namespace hierarchy so I don't have to add a new namespace for ever Area it throws a 404.
namespace My.Web.Controllers
{
[Area("MyArea")]
public class OtherPlaceController : Controller
{
//...
}
}
It seems that the .Areas
portion of the namespace is important but I can't figure out why...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确信您已经习惯了,默认情况下,ASP.NET MVC非常非常依赖于目录结构(控制器需要位于“Controllers”文件夹中,视图位于“Views”文件夹中,等等.) 这只是更多的相同。您的区域应该位于单独的文件夹中 - 这实际上是拥有它们的主要原因之一! :)
As I'm sure you've become accustomed to, by default ASP.NET MVC is very much dependent on directory structure (controllers need to be in "Controllers" folder, views in "Views", etc.) This is just more of the same. Your areas should be in separate folders -- that's actually one of the main reasons to have them! :)