Can't retrieve BadRequest errors when calling .NET CORE 5 Web API microservice from ASP.NET MVC project - asp.net-core-mvc

I am trying to retrieve ModelState validation errors from a micro service built with .NET Core 5 Web API from a ASP.NET Core MVC frontend.
Say I have a model that looks like this:
public class Comment
{
public string Title { get; set; }
[Required]
public string Remarks { get; set; }
}
When I call the rest endpoint in the micro service through Swagger to update the Comment model without a remark, I get a response body like this:
{
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"Remarks": [
"The Remarks field is required."
]
}
}
Awesome! This is what I expect... However, when I call this endpoint through my MVC project, I can't seem to get the actual "errors".
This is how I am calling the rest endpoint:
var client = _httpClientFactory.CreateClient("test");
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(comment), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PutAsync($"api/comments", httpContent);
The response object just has a statusCode of BadRequest. I want to pull the info regarding the errors (the section that says "The Remarks field is required.") but I don't see it in the Content or Headers property or anything like that.
I am new to micro services and .NET Core - am I doing something wrong? Do I need to add something to the startup.cs? Seems weird that I can get the BadRequest status but no supporting problem details.
Thanks in advance!

Be sure your web api controller is not declared with [ApiController].
Web Api Project:
//[ApiController]
[Route("api/[controller]")]
public class CommentsController : ControllerBase
{
[HttpPut]
public IActionResult Put([FromBody] Comment model)
{
if(ModelState.IsValid)
{
//do your stuff...
return Ok();
}
return BadRequest(ModelState);
}
}
Mvc Project:
HttpResponseMessage response = await client.PutAsync($"api/comments", httpContent);
var result = response.Content.ReadAsStringAsync().Result;

Related

ASP.NET core web API routing not supported in AWS HTTP API Gateway?

I am trying to transfer an asp.net core web api to my first AWS HTTP API.
I have hosted the asp.net core web api project as a lambda function, and trying to match the endpoints through the API gateway.
I can access the default endpoints though my API gateway. i.e. the following endpoint can be accessed through my api gateway successfully.
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value10", "value200" };
}
...
}
But, I get a 404 not found exception if I try to access some method with a Route attribute. e.g.
[Route("api/[controller]")]
[ApiController]
public class ReportsController : ControllerBase
{
// GET api/values
[HttpGet, Route("GetReports")]
public IEnumerable<string> GetReports()
{
return new string[] { "value100", "value2000" };
}
}
with the following mapping
What am I doing wrong here?
thanks,

Swagger-Net supporting API Key authentication

We are using token authentication in our WebAPI application. Every call (other then method which obtains key) uses same pattern.
Authorization: our-token v01544b7dce-95c1-4406-ad4d-b29202d0776c
We implemented authentication using Attribute and IActionFilter
Controllers look like so:
[RoutePrefix("api/tms/auth")]
public class AuthController : BaseController
{
public ISecurityService SecurityService { get; set; }
[TokenAuth]
[Route("logout")]
[HttpPost]
public HttpResponseMessage Logout()
{
try
{
this.SecurityService.InvalidateAccessToken(this.StaticContextWrapperService.AccountId, token, HttpContext.Current.Request.UserHostAddress);
// Return OK status
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (LoginException le)
{
return this.LogoutFailureResponse(le.Message);
}
}
private HttpResponseMessage LogoutFailureResponse(string message)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(message, Encoding.UTF8, "text/plain")
};
}
}
Swagger config has following:
c.ApiKey("our-token", "header", "Our Token Authentication");
Swagger UI showing "Authorize" button and I can paste token into field on popup.
However, no headers passed in any tests. And no methods have "lock" icon on them.
EDIT:
I also tried:
c.ApiKey("our-token", "header", "Our Token Authentication", typeof(TokenAuthAttribute));
Where attribute is just attribute:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class TokenAuthAttribute : Attribute
{
}
Then we use IActionFilter to check if attribute applied to method and thats where we check for permission. This is done to use service via DI.
EDIT2:
I made change to how Attribute declared:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class TokenAuthAttribute : AuthorizeAttribute
{
}
After than Swagger UI started to show all methods as secured, so it does analyze that it's in fact AuthorizeAttribute, not just Attribute
After that it started to put header like so:
our-token: ZGV2OnYwMTA2YjZmYjdhLWRlNTUtNDZlNC1hN2Q4LTYxMjgwNTg2M2FiZQ==
Where it should be:
Authorization: our-token GV2OnYwMTA2YjZmYjdhLWRlNTUtNDZlNC1hN2Q4LTYxMjgwNTg2M2FiZQ==
If I'm not mistaken you should have:
c.ApiKey("our-token", "header", "Our Token Authentication", typeof(TokenAuthAttribute));
With that in place, all the actions tagged with TokenAuth should show a lock icon
You can see it in action in one of mine:
https://turoapi.azurewebsites.net/swagger/ui/index
And the code behind that is here:
https://github.com/heldersepu/TuroApi/blob/master/TuroApi/App_Start/SwaggerConfig.cs#L67

Webapi method Get with string parameter not getting invoked

I am creating asp.net webapi with two get methods. One returns all the records while the other should be filtering based on a string parameter called countrycode. I am not sure for what reason the get method with string parameter doesnt get invoked.
I tried the following uri's
http://localhost:64389/api/team/'GB'
http://localhost:64389/api/team/GB
Following is my code
Web API
public HttpResponseMessage Get()
{
var teams = _teamServices.GetTeam();
if (teams != null)
{
var teamEntities = teams as List<TeamDto> ?? teams.ToList();
if (teamEntities.Any())
return Request.CreateResponse(HttpStatusCode.OK, teamEntities);
}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Team not found");
}
public HttpResponseMessage Get(string countryCode)
{
if (countryCode != null)
{
var team = _teamServices.GetTeamById(countryCode);
if (team != null)
return Request.CreateResponse(HttpStatusCode.OK, team);
}
throw new Exception();
}
WebAPIConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/html"));
}
}
I think you are probably hitting the default 'Get()' method from your default API route.
I expect if you changed the parameter name to 'id' on your method like so, it would also work:
public HttpResponseMessage Get(string id)
This is because the optional parameter name in the default route is 'id'.
For attribute routing to work, you need to decorate your controller and methods with the values which were previously inferred by the route configuration.
So at the top of your controller, you would probably have:
[RoutePrefix("api/team")]
public class TeamController : ApiController
Then above your second get method:
[Route("{countryCode}")]
public HttpResponseMessage Get(string countryCode)
Since attribute routing, I haven't used the "old-style" routing.
Check out the ASP.NET page on attribute routing for more information.
Edit for comment:
If you have two routes which have the same parameters you need to differentiate them somehow in the route. So for your example of get by team name, I would probably do something like this:
[HttpGet()]
[Route("byTeamName/{teamName}")]
public HttpResponseMessage GetByTeamName(string teamName)
Your url would then be /api/team/byTeamName/...
Your other method name is "Get" and the default HTTP attribute routing looks for method names with the same as HTTP verbs. However you can name your methods anything you like and decorate them with the verbs instead.

How to return xml or json with ASP.NET web mvc 6 depending on Accept Header

I have implemented a web api controller using ASP.NET mvc 6 and I would like to return the result of the controller as json or xml, depending on the client's Accept header. For example, if the client sends a GET request with "Accept: application/xml", then the returned response should be xml. If the header is "Accept: application/json", then it should be json. At the moment the controller always returns json. Is there a way of configuring this? Note: this question is indeed a duplicate of How to return XML from an ASP.NET 5 MVC 6 controller action. However the solution provided there did not solve my problem. The accepted answer below worked for me.
The controller is given below and is the one provided by the ASP.NET 5 web api template:
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id:int}")]
public string Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
Thanks for your help!
I did the research for you, you may continue alone:
needed:
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final"
startup:
services.Configure<MvcOptions>(options =>
{
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
});
go on from here
another
and antoher

Why am I getting a 404 response from my POST in web api?

I have the following action in my Web api controller:
// POST api/<controller>
[AllowAnonymous]
[HttpPost]
public bool Post(string user, string password)
{
return true;
}
I am getting the following error with a 404 status when hitting it with either fiddler or a test jQuery script:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/amsi-v8.0.0/api/account'.","MessageDetail":"No action was found on the controller 'Account' that matches the request."}
My http route is as follows:
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Gets work fine. I found another question here which talks about removing WebDAV from IIS. I tried that, still same issue.
Why do I get a 404?
The default action selection behavior in ASP.NET Web API cares about your action method parameters as well. If they are simple type objects and they are not optional, you will need to supply them in order to invoke that particular action method. In your case, you should send a request against a URI as below:
/api/account?user=Foo&password=bar
If you wanna get these values inside the request body rather than the query string (which is a better idea), just create a User object and send the request accordingly:
public class User {
public string Name {get;set;}
public string Password {get;set;}
}
Request:
POST http://localhost:8181/api/account HTTP/1.1
Content-Type: application/json
Host: localhost:8181
Content-Length: 33
{"Name": "foo", "Password":"bar"}
And your action method should look like something below:
public HttpResponseMessage Post(User user) {
//do what u need to do here
//return back the proper response.
//e.g: If you have created something, return back 201
return new HttpResponseMessage(HttpStatusCode.Created);
}
When we are posting a json it expect a class so create class in model folder like this
public class Credential
{
public string username { get; set; }
public string password { get;set; }
}
and now change the parameter
[HttpPost]
public bool Post(Credential credential)
{
return true;
}
Try now everything will work smooth

Resources