Last post Feb 01, 2017 01:58 AM by Chris Zhao
Jan 24, 2017 10:43 AMfirstname.lastname@example.org|LINK
I want to make a very very simple Unauthorized 401 error back to my API client from my WEB API but I simply cannot figure it out.
I am getting error messages whatever I try and I have googled for some time but all threads are way overkill for what I want.
I just want something very simple like below but it tells me HTTPResponseMessage cannot be used by a method, so how do I return a simple 401 back to the client without having to use exception filters and middleware, yes I do understand those are preferred
methods but this is a learning excersise for me so please humor me with a simple way of doing it directly in the web api controller.
if (ApiKey != "FooBar")
Jan 24, 2017 03:22 PM|mohtshm|LINK
What I understand is that you could simply achieve this by putting
on your action
Jan 24, 2017 06:28 PMemail@example.com|LINK
Jan 25, 2017 07:34 AM|mohtshm|LINK
yes you should post it by putting [HttpPost] attribute on action method.
Jan 26, 2017 12:20 PM|PatriceSc|LINK
What if you try :
If you have an error it's best to always tell which error you have (so that we can check it's not a typo, for now your code shouldn't even compile, seems you missed the new keyword and the enum name but creating the response from the request seems easier
Also passing data on the query string or using post is not directly related. It might be better to open another discussion else we loose track of the current step and it's s harder to discuss and follow multiple issues in a single discussion.
Feb 01, 2017 01:58 AM|Chris Zhao|LINK
HttpResponseMessage is a way of returning a message/data from your action.
public HttpResponseMessage Get()
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Unauthorized);