MVC 3 不查找区域下的视图

发布于 2024-11-25 09:32:54 字数 469 浏览 1 评论 0原文

我在 MVC 3 中使用多个区域,但遇到了无法找到我的视图的问题。路由似乎正确地选择了我的控制器(所有操作都在没有任何问题的情况下执行),但是当我返回视图时,MVC 根本找不到它。

因此,如果我在名为“Some”的区域中有一个名为“Thing”的简单控制器,并且执行以下操作...

public ActionResult Index()
{
    return View("Index");
}

操作正确执行,但 MVC 找不到视图,我会收到一条消息,内容如下

未找到视图“Index”或其主视图...它将显示所有搜索到的位置,即

~/Views/Thing/Index.cshtml 〜/Views/Shared/Index.cshtml

等,但它不会在

〜/Some/Views/Thing/Index.cshtml

中查找有关我做错了什么的任何想法吗?

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");
}

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

~/Views/Thing/Index.cshtml
~/Views/Shared/Index.cshtml

etc, etc, but it doesn't look in

~/Some/Views/Thing/Index.cshtml

Any ideas on what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

却一份温柔 2024-12-02 09:32:54

好吧,很抱歉必须回答我自己的问题,但没有人真正给我我正在寻找的答案。看来我的问题是自定义路由。

为了重现该问题,我创建了一个空白的 MVC 3 项目,并添加了一个名为“Some”的区域,并在该区域中添加了一个名为“Thing”的控制器。我创建了一个 Index 操作,它只是返回一个视图。然后,我将索引视图添加到 ~/Areas/Some/Views/Thing/Index.cshtml

太棒了。因此,当我点击 /Some/Thing/Index 时,它会正确返回视图。

现在,添加一条到 Global.asax 的路由,如下所示:

routes.MapRoute(
                "Custom", // Route name
                "Bob", // URL with parameters
                new { area = "Some", controller = "Thing", action = "Index" }
                );

现在,当我导航到 /Bob 时,我收到我提到的错误 - MVC 找不到视图。为了解决这个问题,我必须在 SomeAreaRegistration 类而不是 Global.asax 中注册此路由。我也不需要“区域”属性,所以它看起来像这样。

    context.MapRoute(
        "Custom", // Route name
        "Bob", // URL with parameters
        new { controller = "Thing", action = "Index" }
        );

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.

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.

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" }
                );

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" }
        );
揽清风入怀 2024-12-02 09:32:54

如果您的控制器与区域名称相同,则在检查区域路由之前,您的控制器将由默认基本路由 {controller}/{action} 获取因此将在根 /views 中查找视图,而不是在区域 /views 中。重命名该区域或控制器将解决此问题。

If your controller has the same name as the area, your controller will be picked up by the default base route {controller}/{action} BEFORE it checks the area route and therefore will look for the view in the root /views instead of in the area /views. Renaming either the area or the controller will resolve this.

不顾 2024-12-02 09:32:54

尝试在 global.asax 中添加以下路由:

 context.MapRoute(
                "default",
                "Some/{controller}/{action}/",
                new { controller = "Thing", action = "Index"}
            );

Try add following route in global.asax:

 context.MapRoute(
                "default",
                "Some/{controller}/{action}/",
                new { controller = "Thing", action = "Index"}
            );
太傻旳人生 2024-12-02 09:32:54

确保您的“Some”区域中有一个名为 SomeAreaRegistration.cs 的文件。
该文件应包含如下内容:

public class SomeAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Some";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Some_default",
            "Some/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

Make sure that you have a file called SomeAreaRegistration.cs on your "Some" area.
this file should contain something like the following:

public class SomeAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Some";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Some_default",
            "Some/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}
他不在意 2024-12-02 09:32:54

使用区域时,请将 index.cshtml 放在 ~/Areas/Some/Views/Thing/Index.cshtml 位置

When using areas, put your index.cshtml at location ~/Areas/Some/Views/Thing/Index.cshtml

沫离伤花 2024-12-02 09:32:54

您是否检查(通过设置断点)是否调用了控制器方法?

您列出的搜索视图的页面不在“某些”区域中。
从哪里调用链接?

如果您从另一个区域或从根调用该链接,则必须在操作链接中设置该区域:

 @Html.ActionLink("Go To Some/Thing", "Index", "Thing", new {area="Some"}, null)

Did you check (by setting breakpoint) if the controller method is called?

The pages you listed where the view is searched are not in the area "some".
From where do you call the link?

If you call the link from another area or from root you have to set the area in the action link:

 @Html.ActionLink("Go To Some/Thing", "Index", "Thing", new {area="Some"}, null)
假扮的天使 2024-12-02 09:32:54

只是为了在这里添加另一个解决方案。我也遇到了这个问题,但我的问题是由于 Global.asax.cs 中存在冲突的“catch all”路由,

删除此路由解决了该问题。

Just to add another solution here. I was also having this problem but mine was due to having a conflicting "catch all" route in Global.asax.cs

Removing this route fixed the issue.

裸钻 2024-12-02 09:32:54

在 RouteConfig.cs 中尝试此操作。

可能会对某人有帮助..

      routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Controllername", action = "Actionname", id = UrlParameter.Optional },
            namespaces: new [] { "Projectname.Areas.Areaname.Controllers" }

            ).DataTokens.Add("area", "Areaname");

Try this in RouteConfig.cs.

May be it will be helpful to someone..

      routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Controllername", action = "Actionname", id = UrlParameter.Optional },
            namespaces: new [] { "Projectname.Areas.Areaname.Controllers" }

            ).DataTokens.Add("area", "Areaname");
丘比特射中我 2024-12-02 09:32:54

默认情况下,它不会在 *~Some/*Views... 中查找(我也不知道如何强制这样做),约定是 ~/Views/... ...,所以这就是放置视图的正确位置。如果您希望 URL 包含“Some”,您可以更改路由来处理该问题。

It'll not look in *~Some/*Views..... by default (I am not sure how you can force that either), the convention would be ~/Views/......, so that'd be the right place to put the view in. In case you want the URL to contain "Some", you can change the routing to handle that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文