Assume following in .NET MVC controller:
public JsonResult Update(string param1, User user)
{
//Some code here
return null;
}
How would one use RestSharp to do post to this method?
This does not work (controller gets empty user):
var request = new RestRequest("Update", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("param1", "whatever");
request.AddBody(user); //Instance of User
_restClient.Execute(request);
However it will work if controller would have only one parameter "user"
So is this possible to mix parameters in RestSharp?
Related
I am trying to experiment with asp.net core web api so I made some simple api with a controller like this:
[ApiController]
[Route("MyController")]
public class MyController : ControllerBase
{
[HttpGet]
[Route("GetResult")]
public IActionResult GetResult(string param1, string param2= null, SomeClassObj obj = null)
{ .... }
}
I ran the api locally and sent this postman GET request:
https://localhost:5001/MyController/GetResult?param1=someString
I got the error: 415 Unsupported Media Type
What am I missing here so it could work?
I was getting the same error after invoking the WEB API from .NET MVC.
As suggested by #zhulien, I have changed from [FromBody] to [FromForm] in WebAPI, it works fine for me.
.NET Core WebAPI method.
public async Task<IActionResult> Login([FromForm] LoginModel loginInfo)
{ // JWT code here }
.Net Core MVC Action Method.
public async void InvokeLoginAPIAsync(string endPoint, string userName, string pwd)
{
configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
baseUrl = configuration["Application:BaseAPI"] ?? throw new Exception("Unable to get the configuration with key Application:BaseAPI");
string targetUrl = string.Format("{0}/{1}", baseUrl, endPoint);
using (HttpClient deviceClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, targetUrl);
var data = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("userName", userName),
new KeyValuePair<string, string>("password", pwd)
};
request.Content = new FormUrlEncodedContent(data);
using (var response = await deviceClient.SendAsync(request))
{
if (response.StatusCode == HttpStatusCode.OK)
{
TempData["Response"] = JsonConvert.SerializeObject(response.Content);
}
}
}
}
Which version of .NET Core are you using?
Try doing the request from the browser and see if you have the same result.
Also, are you sure you're doing a GET and not a POST request in Postman? You shouldn't get 415 errors for GET requests, especially when you're not sending any body.
This error mainly occurs when you try to send a body and you haven't specified the media-type through the Content-Type header.
Ensure that the request is GET and your body is empty.
Solution after post edit:
As you're trying to parse a DTO object(SomeClassObj), you should specify where the values should come from. In order to fix your specific case, add the [FromQuery] attribute before SomeClassObj.
Your code should look like this:
[ApiController]
[Route("MyController")]
public class MyController : ControllerBase
{
[HttpGet]
[Route("GetResult")]
public IActionResult GetResult(string param1, string param2= null, [FromQuery]SomeClassObj obj = null)
{ .... }
}
This tells the parser to fetch the data from the query string. This will fix the 415 issue. However, if you want to bind to complex types, especially on get, checkout those topics: ASP.NET CORE 3.1 Model Binding and this issue as you will most probably encounter issues with parsing your DTO object.
Use [FromForm] attribute before each argument in the controller function.
I'm confused... I have a very simple Web API and controller, which works fine if I have a GET request, but 404's if I have a POST request.
[RoutePrefix("api/telemetry/trial")]
public class LoginTelemetryController : ApiController
{
[Route("login")]
[HttpPost]
public IHttpActionResult RecordLogin(string appKey) {
using (var context = new Core.Data.CoreContext()) {
context.ActivityLogItems.Add(new Domain.Logging.ActivityLogItem()
{
ActivityType = "Trial.Login",
DateUtc = DateTime.UtcNow,
Key = new Guid(appKey)
});
context.SaveChanges();
}
return Ok();
}
When I post against this in postman, I get:
{
"message": "No HTTP resource was found that matches the request URI 'http://localhost:47275/api/telemetry/trial/login'.",
"messageDetail": "No action was found on the controller 'LoginTelemetry' that matches the request."
}
If I change it to a [HttpGet] and put the appKey as a querystring, all is fine.
My app startup is very simple:
public void Configuration(IAppBuilder app)
{
log4net.Config.XmlConfigurator.Configure();
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.MapHttpAttributeRoutes(); // <------ HERE
FilterConfig.RegisterHttpFilters(httpConfig.Filters);
LoggingConfig.RegisterHandlers(httpConfig.Services);
ConfigureOAuth(app);
ConfigureWebApi(httpConfig);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(httpConfig);
}
Can anyone spot why POST requests aren't being found? Thanks
If I take string parameter out and replace it with a request object, it works...
Instead of: public IHttpActionResult RecordLogin(string appKey)
I create a request model class:
public class PostLoginTelemetryRequest{
public string appKey {get;set;}
}
Then alter the signature:
public IHttpActionResult RecordLogin(PostLoginTelemetryRequest request)
Everything works fine (why it can't take a regular string like MVC5 web dev, I don't know, but anyway...)
(also note that I had tried this in every format from the client with the string method: form-url-encode, raw body, etc, so I'm fairly certain it wasn't a calling format issue).
I was having a look at this link that shows how to migrate from Web API 2 to MVC 6.
I am trying to have Action methods in my controllers with the HttpRequestMessage bound. This works in Web Api 2.
[Route("", Name = "AddTaskRoute")]
[HttpPost]
public Task AddTask(HttpRequestMessage requestMessage, [FromBody]NewTask newTask)
{
var task = _addTaskMaintenanceProcessor.AddTask(newTask);
return task;
}
and the requestMessage contains the details about the Http request such as headers, verb, etc.
I am trying to get the same with MVC 6 but the requestMessage seems to be incorrectly bound and it shows details such as the method being GET when the action is actually a POST. I believe I haven't configured the WebApiCompatShim as per the article suggests so the binding is not properly done. But I do not have the extension method services.AddWebApiConventions(); available in the version "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-rc1-final"
Anybody has succeed when trying this?
PS: The Request property available in my controller seems to have details about the http request, but I'd like to have the HttpRequestMessage instance.
In MVC6, You should be able to use the Request object to get header information.
var contentTypeHeader = Request.Headers["Content-Type"];
It is true that they removed some of the nice methods like Request.CreateResponse() and OK() etc.. But there are some alternatives you can use.
All of these classes we will be using to create a response are inheriting from the ObjectResult base class. So you can use ObjectResult as the return type of your Web api method.
HttpOKObjectResult
In MVC6, You can use create an object of HttpOKObjectResult class and use that as your return value instead of Request.CreateResponse(). This will produce the status code 200 OK for the response.
Web API2 code
public HttpResponseMessage Post([FromBody]string value)
{
var item = new { Name= "test", id = 1 };
return Request.CreateResponse(HttpStatusCode.OK,item);
}
MVC 6 code
[HttpPost]
public ObjectResult Post([FromBody]string value)
{
var item = new {Name= "test", id=1};
return new HttpOkObjectResult(item);
}
Or simply use the OK() method.
[HttpPost]
public ObjectResult Post([FromBody]string value)
{
var item = new {Name= "test", id=1};
return Ok(item);
}
CreatedAtRouteResult
You can use CreatedAtRouteResult class to send a response with 201 Created status code with a location header.
MVC 6 code
[HttpPost]
public ObjectResult Post([FromBody]string value)
{
var item = new { Name= "test", id=250};
return new CreatedAtRouteResult(new { id = 250}, item);
}
The client will receive a location header in the response which will point to the api route with 250 as the value for the id parameter.
HttpNotFoundObjectResult
You can use this class to return a 404 Not found response.
Web API2 code
public HttpResponseMessage Post([FromBody]string value)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
MVC 6 code
[HttpPost]
public ObjectResult Post([FromBody]string value)
{
return new HttpNotFoundObjectResult("Some");
}
I found that to use the Microsoft.AspNetCore.Mvc.WebApiCompatShim, it should be services.AddMvc().AddWebApiConventions() see this example instead of services.AddWebApiConventions() as shown in the docs.
I'm putting in a feedback item on their docs.
I want to test my service proxy class that is calling a MVC WebApi and gets back a JSON response.
public class CarServiceProxy
{
public CarsCollection GetCars()
{
CarsCollection cars = new CarsCollection();
string api = "api/Car/GetCars";
var response = httpClient.GetAsync(api).Result;
if (response.IsSuccessStatusCode)
{
cars = response.Content.ReadAsAsync<CarsCollection >().Result;
}
return cars;
}
}
then my MVC WebSite controller class calls the above proxy class as:
public ActionResult Index()
{
CarsCollection cars = this.carsServiceProxy.GetCars();
return View(cars);
}
Now to test both in isolation, I can test my controller using MOQ and mocking carServiceProxy and faking the CarsCollection with some fake data in my tests. I am ok so far.
But, how do I test the proxy class using MOQ to mock the API response (JSON)? I think I do need to test because the conversion form JSON to C# happens in that class, and if some one changes my model CarsCollection, that might break. So I do need to test the proxy class.
You will want to create a "Fake" HTTP handler for your client. This way you can control exactly what HTTP response is returned. You can inject an HTTP handler when you construct an HTTP client.
For testing I typically use an internal constructor for testing, which accepts an HTTP handler. This way I can easily test my class that consumes an HTTP Client with a fake handler. If you take this approach and your unit tests are in a separate assembly, you'll need to add the following to the AssemblyInfo.cs for your target project:
[assembly: InternalsVisibleTo("NameSpace.MyUnitTestProject")]
Fake Handler:
public class FakeHttpMessageHandler : HttpMessageHandler
{
private HttpResponseMessage _response;
public FakeHttpMessageHandler(HttpResponseMessage response)
{
_response = response;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var responseTask = new TaskCompletionSource<HttpResponseMessage>();
responseTask.SetResult(_response);
return responseTask.Task;
}
}
And then to consume (may want to use JSON serialisation helpers here):
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(#"{'Cars':[{'Name':'BMW'}]");
var handler = new FakeHttpMessageHandler(response);
var client = new HttpClient(handler);
// client.GetAsync().result will return the response
You can change the httpClient.GetAsync(api).Result part to be moackable and testable to test the proxy class. In other words, you can make your code independent of HTTP request response and just work on JSON data for testing.
How do i acces my methods in my webapi when there are multiple get,post and delete methods from my console application, how do i differentiate them this is my api controller
public IQueryable<Store> GetAll()
{
return StoreRepository.All;
}
//GetAll Stores including all relation tables
public IQueryable<Store> GetAllIncluding()
{
return StoreRepository.AllIncluding();
}
//Get store by id/id=5
public Store Find(long storeid)
{
stores = StoreRepository.Find(storeid);
return stores;
}
//Insert or Update Store
public void InsertorUpdateWithGraph(Store store)
{
StoreRepository.InsertOrUpdateWithGraph(store);
}
//Insert or Update StoreDetail
public void InsertOrUpdateStoreDetail(StoreDetail storedetail)
{
StoreRepository.InsertOrUpdateStoreDetail(storedetail);
}
//Get StoreDetail by id/id=5
public StoreDetail FindStoreDetail(long storedetailid)
{
storedetail = StoreRepository.FindStoreDetail(storedetailid);
return storedetail;
}
public List<StoreDetail> GetAllStoreDetails(long storedetailid)
{
List<StoreDetail> storedetails = StoreRepository.GetAllStoreDetails(storedetailid);
return storedetails;
}
public Sage FindSage(long sageid)
{
return StoreRepository.FindSage(sageid);
}
like this i may have more than two get,post,insert or update methods i have to acces this methods from my console application how can i map the methods i want,cana any one help me here how will i define the routes for this
You can have multiple "Get..." actions and you can get away without HttpGet attribute because they start with "Get". "Find..." methods need to be decorated with HttpGet
Those "Insert..." you need to decorate with HttpPost or HttpPut attributes.
Parameters to these methods can be configured in two ways. You can POST object like {id:"ddd",name:"nnn"} to action like
MyAction(int id, string name)
Web APi framework threats any methods that start with Post..., Delete..., Get..., Put... as corresponding Http Verbs. But you can name them the way you with and then decorate with Http attributes.
When it comes to parameters, it is about a correlation of your controller actions to the routes.
And now, to run it from the console application you can use HttpClient
string _webSiteUrl = "www.ffsdfds.com"
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(_webSiteUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // for posting
HttpResponseMessage resp = httpClient.GetAsync("/api/area/getall").Result;