【问题标题】:How can I set up my default view in MVC4 to point to a specific area, controller and action?如何在 MVC4 中设置我的默认视图以指向特定区域、控制器和操作?
【发布时间】:2013-01-11 15:40:52
【问题描述】:

我有一个用户区域,在其中我注册了以下内容:

context.MapRoute("DefaultRedirect",
    "",
    new { controller = "Account", action = "Login" }
);  

当我使用 routeDebug 时,它告诉我,当我连接到我的网站 www.xxx.com 时,它会尝试调用

area = User, controller = Account, action = Login

当我使用以下方式直接连接时:www.xxx.com/User/Account/Login 我的登录页面出现。

当我不使用 routeDebug 并连接到我的网站 www.xxx.com 时,我会收到一条错误消息:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /

这是我的控制器操作方法:

   public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login()
        {
            ViewBag.ReturnUrl = "xx";
            return View("~/Areas/User/Views/Account/Login.cshtml");
        }

我很困惑,因为 routeDebug 似乎表明我将转到正确的控制器和操作,但是当我不使用它并放置断点时,它似乎并没有转到控制器操作。

【问题讨论】:

  • 你能验证 ~/Areas/User/Views/Account/Login.cshtml"); 存在于这个位置 - 这可能是错误。我只涉足 MVC,但如果你这样做应该可以只需 return View(); 因为这将解析为帐户控制器的登录视图。
  • 如果我直接使用 www.xxx.com/User/Account/Login 调用操作,它就在那里并且它可以工作,但如果我调用 www.xxx.com 则不会

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 routes


【解决方案1】:

如果这个控制器在同一个区域内,我认为你可以使用

[AllowAnonymous]
    public ActionResult Login()
    {
        ViewBag.ReturnUrl = "xx";
        return View();
    }

无论哪种方式,如果您只有不同区域的视图,您可以使用

return View("~/Views/YourArea/YourController/YourView.aspx");

【讨论】:

    【解决方案2】:
     return RedirectToAction("Login", "Account");
    

    将重定向到特定控制器和特定操作。 如果帐户在文件夹中更深,只需包含路径

    【讨论】:

    • 对不起。我不明白你的建议。你建议我把这段代码放在哪里?谢谢
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2015-12-05
    相关资源
    最近更新 更多