2

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.

1
  • You might be better off returning a result with a redirect url to your view. Commented May 17, 2012 at 18:16

2 Answers 2

3

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;
    
Sign up to request clarification or add additional context in comments.

Comments

0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.