MVC3 到区域的默认路由不会搜索区域内的视图

发布于 2024-12-21 12:26:42 字数 1408 浏览 1 评论 0原文

我将默认路由对象设置为区域(也称为“Beheer”)内的控制器(“Beheer”)。

像这样:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

它可以在区域内找到该控制器和操作,但它找不到视图,因为它只在这些位置查找:

~/Views/Beheer/Index.aspx
~/Views/Beheer/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Beheer/Index.cshtml
~/Views/Beheer/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

虽然它应该在这个位置查找:

~/Beheer/Views/Beheer/Index.aspx

我怎样才能让它搜索那里的视图?

我已经尝试过:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { area = "Beheer", controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

并且我尝试过这个(使用名称空间):

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Beheer", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new[] { "Areas.Beheer" }
    );

但没有任何变化。它在正确的控制器中输入正确的操作,但找不到视图。

I set the default route object to a controller ("Beheer") inside an area (also called "Beheer").

Like this:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

It can find that controller and the action fine inside the Area, but it can not find the view because it only looks in these locations:

~/Views/Beheer/Index.aspx
~/Views/Beheer/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Beheer/Index.cshtml
~/Views/Beheer/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

While it should be looking in this location:

~/Beheer/Views/Beheer/Index.aspx

How can I make it search for the view there?

I already tried:

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { area = "Beheer", controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

And I tried this (with namespaces):

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Beheer", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new[] { "Areas.Beheer" }
    );

But nothing changes. It enters the correct action in the correct controller but can't find the view.

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

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

发布评论

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

评论(2

浪漫人生路 2024-12-28 12:26:42

您应该在区域注册中添加您的路线。 BeheerAreaRegistration 有一个设置区域名称的属性。

 
    public class BeheerAreaRegistration : AreaRegistration
    {
       public override string AreaName
       {
         get
         {
           return "Beheer";
         }
        }

    public override void RegisterArea(AreaRegistrationContext context)
    {
       context.MapRoute( "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults);
    }

You should add your route in the area registration. BeheerAreaRegistration has a property that sets the area name.

 
    public class BeheerAreaRegistration : AreaRegistration
    {
       public override string AreaName
       {
         get
         {
           return "Beheer";
         }
        }

    public override void RegisterArea(AreaRegistrationContext context)
    {
       context.MapRoute( "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Beheer", action = "Index", id = UrlParameter.Optional } // Parameter defaults);
    }
空‖城人不在 2024-12-28 12:26:42

我遇到了这个问题,发现我需要完全限定命名空间,并且在找到正确的视图时遇到问题,直到我终止了 IIS 进程、某种奇怪的缓存或其他可能的东西。

new[] { "Areas.Beheer" }

可能会变成

new[] {"myApp.Areas.Beheer.Controllers"}

也许你的问题和我的类似 - 也许不是。

I had this problem and found I needed to fully qualify the namespaces and I has issues with it finding the right view until I killed the IIS process, some sort of odd caching or something maybe.

new[] { "Areas.Beheer" }

might become

new[] {"myApp.Areas.Beheer.Controllers"}

maybe your problem is similar to mine - maybe not.

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