Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:03:05+00:00 2026-06-02T01:03:05+00:00

Investigating the Web API as part of an MVC 4 project as an alternative

  • 0

Investigating the Web API as part of an MVC 4 project as an alternative way to provide an AJAX-based API. I’ve extended AuthorizeAttribute for the MVC controllers such that, if an AJAX request is detected, a JSON-formatted error is returned. The Web API returns errors as HTML. Here’s the AuthorizeAttribute that I’m using with the MVC controllers:

public class AuthorizeAttribute: System.Web.Mvc.AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        base.HandleUnauthorizedRequest(filterContext);

        filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary 
            {
                { "area", "" },
                { "controller", "Error" },
                { "action", ( filterContext.HttpContext.Request.IsAjaxRequest() ? "JsonHttp" : "Http" ) },
                { "id", "401" },
            });
    }
}

How could I reproduce this to provide equivalent functionality for the Web API?

I realize that I need to extend System.Web.Http.AuthorizeAttribute instead of System.Web.Mvc.AuthorizeAttribute but this uses an HttpActionContext rather than an AuthorizationContext and so I’m stuck by my limited knowledge of the Web API and the seemingly incomplete documentation on MSDN.

Am I even correct in thinking that this would be the correct approach?

Would appreciate any guidance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:03:07+00:00Added an answer on June 2, 2026 at 1:03 am

    To get the equivalent functionality in a Web API filter you can set the HttpActionContext.Response property to an instance of HttpResponseMessage that has the right redirect status code and location header:

    protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) {
        var response = new HttpResponseMessage(HttpStatusCode.Redirect);
        response.Headers.Location = new Uri("my new location");
        actionContext.Response = response;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been investigating what data layer to use for a new web-based project I'm
I am investigating a problem calling a web service from AJAX, which intermittently faults
I have been investigating building web parts for sharepoint 2010 and currently have a
I am investigating the use of web frameworks with my Java web-app. My basic
I am investigating a way to use bamboo to distribute complex environment changes to
I am investigating since a few hours the best way to use the Email
I'm investigating the possibility of utilizing my web host as a cruise control.net build
I have been investigating a JSF upgrade on our existing web apps that are
I am currently investigating using the selenium-on-rails plug-in for testing an upcoming web app
I've just started investigating the Microsoft Ajax Minifer 4.0 for use with a Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.