asp.net-mvc2 - 返回 View() 时控制器不考虑区域

发布于 2024-12-04 14:05:46 字数 642 浏览 0 评论 0原文

当在驻留在某个区域的控制器中调用return View()时,它会尝试在主~/Views/{Controller}/文件夹和主< code>~/Views/Shared/ 文件夹。它不会在 ~/{Area}/Views/{Controller}/ 文件夹中查找。

我尝试将 area 的路线值添加到 MapRoute 函数,还尝试将“area”的“datatoken”添加到路线的 DataTokens 属性。

我在这里错过了什么吗?

这是 MapRoute 调用:

routes.MapRoute("Product", "Products/{GroupName}/{CategoryId}/{CategoryName}/{ProductId}/{ProductName}/{PageName}", New With {.Area = "Products", .controller = "Products", .action = "Product", .PageName = ""}, New With {.CategoryId = "[0-9]*", .ProductId = "[0-9]*"})

When calling return View() in a controller that resides in an area it tries to locate the views in the main ~/Views/{Controller}/ folder and the main ~/Views/Shared/ folder. It doesn't look in the ~/{Area}/Views/{Controller}/ folder.

I tried adding a routevalue for area to the MapRoute function and also tried adding a 'datatoken' for "area" to the Route's DataTokens property.

Am I missing something here?

This is the MapRoute call:

routes.MapRoute("Product", "Products/{GroupName}/{CategoryId}/{CategoryName}/{ProductId}/{ProductName}/{PageName}", New With {.Area = "Products", .controller = "Products", .action = "Product", .PageName = ""}, New With {.CategoryId = "[0-9]*", .ProductId = "[0-9]*"})

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

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

发布评论

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

评论(2

梦在深巷 2024-12-11 14:05:46

您好,要查看的一般内容:

您是否注册了区域映射和区域映射?
两个位置的控制器名称相同吗?

global.asax 中的区域映射示例

参见行:AreaRegistration.RegisterAllAreas(); // 这是您需要做的部分

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

}

protected void Application_Start()
{

    AreaRegistration.RegisterAllAreas();  //  This is part you neeed to do
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}

}

并且每个区域都必须在某处设置映射或映射文件。
此处的示例

名称空间项目
{
///
/// 管理区域注册
///
公共类 AdministratorAreaRegistration :AreaRegistration
{
///
/// 获取要注册的区域的名称。
///
/// 要注册的区域的名称。
公共覆盖字符串区域名称
{
得到
{
返回“管理员”;
}

    /// <summary>
    /// Registers an area in an ASP.NET MVC application using the specified area's context information.
    /// </summary>
    /// <param name="context">Encapsulates the information that is required in order to register the area.</param>
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Administrator_default",
            "Administrator/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional });
    }
}

如果

的路由有问题,请检查此。

我建议使用本指南,并为每个路由创建测试:
http://haacked.com/archive /2007/12/17/testing-routes-in-asp.net-mvc.aspx

Hi general stuff to look at:

Have you registered area mapping and area mapping?
have you same name for controller in both locations?

Example of area mapping in global.asax

see line : AreaRegistration.RegisterAllAreas(); // This is part you neeed to do

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

}

protected void Application_Start()
{

    AreaRegistration.RegisterAllAreas();  //  This is part you neeed to do
    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}

}

and every area has to have mapping or mapping file somewhere set up.
Example here

namespace Project
{
///
/// Registration of administration area
///
public class AdministratorAreaRegistration : AreaRegistration
{
///
/// Gets the name of the area to be registered.
///
/// The name of the area to be registered.
public override string AreaName
{
get
{
return "Administrator";
}
}

    /// <summary>
    /// Registers an area in an ASP.NET MVC application using the specified area's context information.
    /// </summary>
    /// <param name="context">Encapsulates the information that is required in order to register the area.</param>
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Administrator_default",
            "Administrator/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional });
    }
}

}

if you have problem with the routing check this.

I would recomend to use this guide, and create tests for every routing:
http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx

定格我的天空 2024-12-11 14:05:46

正如我们通常对区域所做的那样,我们添加一个 AeaRegistraion 文件 (FooAreaRegistration.cs) 并重写 AreaName 和 RegisterArea() 方法。我们将 AreaName 设置为 AearRegistration 文件所在文件夹相对于全局应用程序的相对路径。以下是示例。

public class FooAreaRegistration : AreaRegistration
{
    /// <summary>
    /// Get the Area Name
    /// </summary>
    public override string AreaName
    {
        get { return "ParentApp/AreaFolder"; }
    }

    /// <summary>
    /// Map and Route the area
    /// </summary>
    /// <param name="context"></param>
    public override void RegisterArea(AreaRegistrationContext context)
    {
        if (null != context)
        {
            //context.maproute goes here              
        }            
    }
}

As we usually do with Areas, we add an AeaRegistraion file (FooAreaRegistration.cs) and override AreaName and RegisterArea() method. AreaName we set to the relative path of the folder where AearRegistration file resides with respect to the global application. Following is example.

public class FooAreaRegistration : AreaRegistration
{
    /// <summary>
    /// Get the Area Name
    /// </summary>
    public override string AreaName
    {
        get { return "ParentApp/AreaFolder"; }
    }

    /// <summary>
    /// Map and Route the area
    /// </summary>
    /// <param name="context"></param>
    public override void RegisterArea(AreaRegistrationContext context)
    {
        if (null != context)
        {
            //context.maproute goes here              
        }            
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文