开发者

Returning to Calling Page Within an Area Using UrlReferrer

I have created a new MVC2 project using the ASP.NET MVC2 Web Application, which gives me the default web site. I have added a simple area in which I had a Home Controller and an Index view. That cuased the first problem with the compiler giving "Multiple types were found that match the Controller name 'Home'". I changed Home to Main and it compiled.

I added a new tab to reference the Index view in my area, opened the website and started clicking the tabs. When I visited the Area index page, I couldn't go back to the Home or About page without changing the menu, as follows:

<ul id="menu">              
     <li><%= Html.ActionLink("SampleArea.Main", "Index", "Main", new { area = "SampleArea" }, null)%></li>
     <li><%= Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)%></li>
     <li><%= Html.ActionLink("About", "About", "Home", new { area = "" }, null)%></li>
</ul>

I could then cycle through the tabs correctly. I then changed the code in the LogOff view in the Account controller, as follows:

public ActionResult LogOff()
{
    FormsService.SignOut();

    //return RedirectToAction("Index", "Home");
    return RedirectToAction(Request.UrlReferrer.AbsolutePath.ToString());
}

I am using UrlReferrer.AbsolutePath to return to the calling page if the User logs off. If the calling page happens to be the View in SampleArea, .AbsolutePath contains "/SampleArea". This is because the controller and view are the defaults, and so they are not included. As it continues, I get the following error message:

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: /Account/SampleArea

My understanding is that /Account has been added because that is the controller it was in when LogOff was executed. It thinks /SampleArea is an action and therefore added the current controller to complete the route.

Using UrlReferrer.AbsolutePath, is there any way I can specify SampleArea as an area, or is there something else I can do to return to the correct page?

New Addition

This is even stranger than I thought. I opened the website I am currently developing and changed the return statement in view LogOut to return using AbsolutePath. A breakpoint reveals it contains "/Club/PrivacyPolicy". However, I get the same error message with the following difference:

Requested URL: /Login/Club/PrivacyPolicy

Why on earth should it prefix it with /Login which is a View, rather than /Account which is a Controller? In fact, w开发者_如何学Chy should it prefix it with anything at all? /Club/PrivacyPolicy is a valid route in Global.asax.cs.


I have finally figured out how to return to the page you were on when you triggered the LogOn or LogOut request. I have used the following piece of code.

Html.ActionLink("Member LogOn", "LogOn", "Account", new { area = "", returnUrl = HttpContext.Current.Request.RawUrl }, null)

This generates

<a href='/LogIn/LogOn?returnUrl=%2FContactUs'>Member LogOn</a>

for example.

In my HttpPost ActionResult LogOn, I then have

if (!String.IsNullOrEmpty(returnUrl))
{
    return Redirect(returnUrl);
}
    else
{
    return RedirectToAction("Index", "Home");
}

Sometimes I find I just need HttpContext.Request.RawUrl.

I'm not quite sure why it has generated /LogIn/LogOn instead of /Account/LogOn, but it works as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜