【发布时间】:2013-05-23 08:25:47
【问题描述】:
我想尝试使用区域(只是尝试)。我在我的项目中添加了一个区域foo:右键单击该项目然后添加区域。
该文件夹包含子文件夹,我可以在其中添加控制器、视图、模型等。它还有一个 cs 文件fooAreaRegistration.cs,用于完成区域路由。
using System.Web.Mvc;
namespace AreasExample.Areas.foo
{
public class fooAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "foo";
}
}
public override void RegisterArea(AreaRegistrationContext context )
{
context.MapRoute(
"foo_default",
"foo/{controller}/{action}/{id}",
new {controller = "Foo", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Global.asax已经有app start中注册区域的功能
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
然后我还创建了一个控制器Foo,它已经有一个默认的Index 动作,之后我向动作添加了一个视图。基于fooAreaRegistration.cs 中的context.MapRoute,如果我运行程序并转到此链接http://localhost:54421/foo/Foo,它不应该工作吗?
当我去我的区域foo 和控制器Foo 时,它显示了一些错误。
错误说
Server Error in '/' Application.
[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to
[B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.
我有什么遗漏吗?我需要添加一些东西吗?
编辑:我不确定是否应该删除这篇文章,因为我找到了下面提到的答案。但是对于那些正在阅读同一本书的人(ASP.NET MVC4 in action)可能会有所帮助。
建议?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-areas