ASP.NET MVC 3 Redirect Requests to Root Page

Ian Daz

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.

arserbin3

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")]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redirect() vs RedirectPermanent() in ASP.NET MVC

ASP.NET MVC 5 : Endless redirect to the login page using the site template

How to force all requests in ASP.NET 5 MVC project to redirect to root "/"?

Redirect to new page from Server for ASP.Net MVC Ajax Request

ASP MVC redirect to error page from controller

Redirect to custom Windows Authentication login page if user anonymous in ASP.NET MVC

Username auto fill in Login Page after Register success (Redirect to Login) in asp.net mvc

Redirect to previous page URL in asp.net core using MVC - c#

ASP.NET MVC 3 - Redirect Loop Only on Certain Browsers

Asp.net redirect to another page

Redirect Error in ASP.NET MVC 5

Asp.net mvc Redirect to same page after login

check if user requests his own page in asp.net mvc/angularjs

Asp.Net MVC - redirect to same page without passing parameter

ASP.NET MVC Redirect to target page or redirect to previous page after login

Redirect user to custom page when s/he is not authorized when using ADFS and ASP.NET MVC

Redirect Page based on dropdown information in ASp.NET mvc

Redirect to HTTPS Error - ASP.NET MVC

Cant do redirect (ASP.NET MVC)

Redirect to "You Are Forbidden" page without changing URL ASP.NET MVC

Using javascript to redirect Asp.net MVC

ASP.NET MVC SqlException does not redirect to error page

Redirect to error page from ajax call in ASP.net MVC

Cannot redirect another page in ASP.NET Core MVC project Controller

Redirect to Home/Error View Page in ASP.NET Core 2.0 MVC

Asp.net Mvc 5, two account controller and Area but redirect to wrong login page

Redirect Page With JQuery In Asp.net Core

Redirect to page OnActionExecuting method ASP.NET Core 5 MVC

ASP.NET Core 6.0 MVC Redirect to page at Program.cs