2013-04-21 91 views
1

在ASP.NET MVC 3项目中使用区域时,我偶然发现了ActionLink和RedirectToAction方法的这个问题。ASP.NET MVC区域 - ActionLink和RedirectToAction

我加在的AccountController下面的代码是在根级...

[HttpPost] 
public ActionResult LogOn(LogOnModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     if (Membership.ValidateUser(model.UserName, model.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
      if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       if (Roles.Provider.IsUserInRole(model.UserName, "Admin")) 
       { 
        return RedirectToAction("Index", "Admin", new { area = "Admin" }); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       }       
      } 
     } 
     else 
     { 
      ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     } 
    } 

基于其当前登录用户的角色属于我重定向到适当的区域。它到目前为止正确工作。

管理方面看起来如下...

enter image description here

在这方面,我已经从根本上复制的_ViewStart.cshtml

注销的联系,关于首页等不工作作为它们指向不存在的路径。

enter image description here

我不想在区域文件夹中创建另一个帐户,或家庭控制器。我想使用根中的那个。

继收到的意见,就改变_LogOnPartial.cshtml代码所示...

@if(Request.IsAuthenticated) { 
    <text>Welcome <strong>@User.Identity.Name</strong>! 
    [ @Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }) ]</text> 
} 
else { 
    @:[ @Html.ActionLink("Log On", "LogOn", "Account", new { area = "" }) ] 
} 

产生以下网址...

enter image description here

这仍然是不对。

+0

'新{面积= “”}'应该给你的根。 – gdoron 2013-04-21 01:52:26

回答

1

提高可gdoron和贾森建议通过改变_LogOnPartial.cshtml代码如下作品的解决方案......

@if(Request.IsAuthenticated) { 
    <text>Welcome <strong>@User.Identity.Name</strong>! 
    [ @Html.ActionLink("Log Off", "LogOff", "Account", new { area = "" }, null) ]</text> 
} 
else { 
    @:[ @Html.ActionLink("Log On", "LogOn", "Account", new { area = "" }, null) ] 
} 

同样,也改变了ActionLink的参数首页关于菜单项_Layout.cshtml如下...

<div id="menucontainer"> 
     <ul id="menu"> 
      <li>@Html.ActionLink("Home", "Index", "Home", new { area = ""}, null)</li> 
      <li>@Html.ActionLink("About", "About", "Home", new { area = "" }, null))</li> 
     </ul> 
    </div> 

的链接显示正确,现在的工作......

enter image description here

3

在rool级别的区域将为new { area = "" }。空的字符串。