MVC 3不找下区意见 [英] MVC 3 does not look for views under Areas
问题描述
我使用多个领域MVC 3和我在与未找到我的看法的问题。路由似乎正确地拿起我的控制器(所有的动作没有任何问题执行),但是当我返回一个视图MVC简单犯规找到它。
I'm using multiple areas in MVC 3 and I'm having problems with my views not being found. The routing seems to pick up my controllers correctly (all the actions are executing without any problems), but when I return a view MVC simply doesnt find it.
所以,如果我在称为区域有一个名为东西简单的控制器有些',我做以下...
So if I have a simple controller called 'Thing' in an area called 'Some' and I do the following...
public ActionResult Index()
{
return View("Index");
}
动作正确执行,但MVC没有找到的看法,我会得到一个消息,说像
The action is executed correctly, but MVC doesn't find the view and I'll get a message saying something like
视图索引或它的主人没有被发现......它会告诉我所有的搜索位置,这将是
The view 'Index' or it's master was not found... And it will show me all the searched locations, which will be
〜/查看/事/ Index.cshtml
〜/查看/共享/ Index.cshtml
~/Views/Thing/Index.cshtml ~/Views/Shared/Index.cshtml
等,等,但它并不在
〜/有/查看/事/ Index.cshtml
~/Some/Views/Thing/Index.cshtml
这是什么,我做错了任何想法?
Any ideas on what I am doing wrong?
推荐答案
好吧,抱歉地回答我的问题,但没有人真的给了我一直在寻找答案。看来我的问题是与自定义路由。
Ok, sorry to have to answer my own question but nobody really gave me the answer I was looking for. It seems my problem was with custom routing.
要重现问题,我创建了一个空白的MVC 3项目,并增加了一个叫做某些和一个名为东西的区域控制器的区域。在事我创建了简单的返回一个视图索引操作。然后,我添加了索引视图到〜/区域/部分/查看/事/ Index.cshtml
To recreate the problem, I created a blank MVC 3 project and added an Area called 'Some' and a controller in that area called 'Thing'. On thing I created an Index action which simply returned a view. I then added the Index view to ~/Areas/Some/Views/Thing/Index.cshtml
大。所以,当我打了/有的/事/索引它返回正确的看法。
Great. So when I hit /Some/Thing/Index it returns the view correctly.
现在去,并加入到Global.asax中的路由看起来是这样的:
Now go and add a route to Global.asax that looks like this:
routes.MapRoute(
"Custom", // Route name
"Bob", // URL with parameters
new { area = "Some", controller = "Thing", action = "Index" }
);
现在,当我浏览到/鲍勃,我得到我提到的错误 - MVC没有找到视图。为了解决这个问题,我不得不注册在SomeAreaRegistration类而不是Global.asax中这条路线。我也并不需要'区域'属性,所以它看起来是这样的。
Now when I navigate to /Bob I get the error I mentioned - MVC doesn't find the view. To fix this problem I had to register this route in the SomeAreaRegistration class instead of Global.asax. I also didn't need the 'area' property, so it looks like this.
context.MapRoute(
"Custom", // Route name
"Bob", // URL with parameters
new { controller = "Thing", action = "Index" }
);
这篇关于MVC 3不找下区意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!