区域MVC 5内的控制器和视图的奇怪行为

时间:2015-04-20 12:03:44

标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-areas

我有一个名为user的区域。

区域配置如下所示

context.MapRouteLowercase(name: "User_Member", url: "User/Member", defaults: new { controller = "User", action = "Member", id = UrlParameter.Optional });

当我浏览此页面而未传入ID时,它会返回我的网址看起来像/user/member

的视图

当我在浏览器/user/member/1

中输入以下内容时

我在

上设置了一个断点

var userId

它被点击了,我检查了id参数及其1是正确的。

    [HttpGet]
    [Authorize]
    public ActionResult Member(Int64 id = 0)
    {
        var userId = id != 0 ? id : ReturnUserId();

        var model = _userProfileBusinessLayer.GetProfile(userId);

        return View(model);
    }

然而,当我按F5时,我得到以下页面

Server Error in '/' Application.
The view 'member' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/user/member.aspx
~/Views/user/member.ascx
~/Views/Shared/member.aspx
~/Views/Shared/member.ascx
~/Views/user/member.cshtml
~/Views/user/member.vbhtml
~/Views/Shared/member.cshtml
~/Views/Shared/member.vbhtml

我不确定为什么我会看到这一点,因为我所做的只是将/1添加到网址?

我应该看到用户的个人资料与id匹配,但我删除了/1并返回视图?!?!?!?!有点困惑

1 个答案:

答案 0 :(得分:1)

在您的项目中,区域user应该有一个继承自AreaRegistration的类,例如

public class UserAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "user";
        }
    }
}

在该课程中,您可以为该区域定义特定路线:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "UserManagement",
        "user/Admin",
        new { controller = "UserAdmin", action = "Index" });
}

Global.asax.cs文件应该有一个Application_Start()方法,该方法调用:

AreaRegistration.RegisterAllAreas();

这将获取您已配置的区域路线。这通常是区域路由的配置方式。您的项目是否有类似的设置?