nunu
nunu

Reputation: 3252

Return View from Asp.Net MVC 4 Web API

As I am working on Asp.Net MVC 4 Web API, I also need to return a View on specific condition. My flow is as given below:

  1. Third party application will call my Web API method. (e.g. User Authentication)
  2. If there is a valid Credential, I need to return HTTP status as a SUCCESS (200)
  3. If user is Invalid, I need to return a VIEW (Login View) from the same MVC application to ask for Credential. (in the view there will be a 'Register' link)

So the point is, Is this possible to return both the LOGIN View (html) and a WebResponse Status (200) on different condition by the same WEB API method?

Thanks in advance.

Upvotes: 2

Views: 8995

Answers (2)

lucask
lucask

Reputation: 2290

You could do two things:

  1. Use AuthorizeAttribute like in Jon Galloway's tutorial: Getting started with WebAPI
  2. Wrap Your response data in HttpResponseMessage and redirect user if needed. For example:

    var responseMessage = new HttpResponseMessage<Stock>(null, HttpStatusCode.RedirectMethod);
    responseMessage.Headers.Location = new Uri("http://www.mysite.com/login");
    
    return responseMessage;
    

Upvotes: 3

robrich
robrich

Reputation: 13205

You should be able to, provided your method's return type is ActionResult. After all, Web API just derives from Controller. The bigger question though: can your client handle such varried answers to the same question? Perhaps you should return 200 on success and non-200 on "go ask the other method for the view".

Upvotes: 0

Related Questions