简体   繁体   中英

ASP.NET MVC 3 Redirect Requests to Root Page

I'm currently working on a 1 page website, and I'm having the following issue.

This website has 5 sections and we would like to create links for each of the sections in the page without using #/hash linking. We went about this by using the PJAX plug-in. So for example if the user clicks on About link, it will use PJAX-plugin to change the URL and add to the push-state... that works great. However when we go directly to the URL for example /About we want to be able to "catch" this request and route the page request back onto the homepage (and use jquery to scroll to the About div). As the actual /About page has no content and is just used as a dummy-page for the PJAX calls.

Thanks.

When a PJAX request is sent, it is always supposed to include this header in the HTTP Request:

X-PJAX: true

You can redirect the different requests by adding a custom ActionMethodSelectorAttribute that checks if the X-PJAX header is present.

Add this custom Attribute class to your solution:

public class AcceptHeaderAttribute : ActionMethodSelectorAttribute
{
    public string HeaderName { get; set; }

    public string HeaderValue { get; set; }

    public AcceptHeaderAttribute(string headerName, string headerValue = null)
    {
        if (String.IsNullOrWhiteSpace(headerName))
            throw new ArgumentNullException("headerName");

        HeaderName = headerName;
        HeaderValue = headerValue;
    }

    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
    {
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        var incomingHeader = controllerContext.HttpContext.Request.Headers[HeaderName];

        var headerExists = String.IsNullOrWhiteSpace(incomingHeader);

        // NOTE: can optionally change this to Contains, or add a parameter for that.
        var valueCorrect = String.Equals(incomingHeader, HeaderValue, StringComparison.OrdinalIgnoreCase);

        return (String.IsNullOrWhiteSpace(HeaderValue)
            ? headerExists
            : valueCorrect);
    }
}

You can then set different Action for PJAX requests versus direct user requests:

[AcceptHeader("X-PJAX", "true")]
public ActionResult About(string myInput)
{
    ViewBag.Test = "This is a PJAX request";

    return View();
}

public ActionResult About()
{
    ViewBag.Test = "This is a normal request";

    return View();

    // or redirect them to root page
    return RedirectToAction("Index");
}

If you only care to check that the Header exists in the HTTP Request, and don't care that the value is correct, you can simplify the usage to just:

[AcceptHeader("X-PJAX")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM