1

While using Areas in ASP.NET MVC 3 project, I stumbled across this problem to do with ActionLink and RedirectToAction method.

I added the following code in the AccountController which is at the Root level ...

[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.");
        }
    }

Based on the Role which the currently logging in User belongs to, I redirect to the appropriate area. It's working correctly up to this point.

The Admin Area looks as follows ...

enter image description here

In this area, I had copied the _ViewStart.cshtml from the root.

The links for Log Off, About, Home etc dont work as the route which they point to doesn't exist.

enter image description here

I don't want to create another Account, or Home controller in the Areas folder. I would like to use the one in the root.

Following the advice received, on changing the _LogOnPartial.cshtml code as shown ...

@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 = "" }) ]
}

produces the following URL ...

enter image description here

which is still not right.

4

2 に答える 2

1

_LogOnPartial.cshtmlコードを次のように変更して、gdoron と Jasen によって提案されたソリューションを改善すると機能します ...

@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) ]
}

同様に、_Layout.cshtmlのHomeおよびAboutメニュー項目のActionLinkパラメータも次のように変更しました...

    <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>

リンクは正しく表示され、現在機能しています...

ここに画像の説明を入力

于 2013-04-21T10:49:07.537 に答える