Web API POST method - asp.net-mvc

Route Config
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
POST-method
[HttpPost]
public Probabilities Post(string word, string userId) {
Request request = new Request();
request.Identify(word, userId);
Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
return probabilities;
}
Im trying to post some data, shown on the screenshot
And im getting an error
"No action was found on the controller 'Identification' that matches the request."
What is happening? How to post 2 simple strings and get the result

While not mandatory, you should follow good practice and encapsulate the payload into a model.
public class MyModel {
public sting word { get; set; }
public sting userId { get; set; }
}
You can then use the FromBody parameter attribute to bind the model to the data sent.
Finally addressing the resource not found issue, ensure that the controller is following the proper convention given that the OP is configured with convention-based routing.
public class IdentificationController : ApiController {
[HttpPost]
public Probabilities Post([FromBody] MyModel model) {
string word = model.word;
string userId = model.userId;
Request request = new Request();
request.Identify(word, userId);
Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
return probabilities;
}
}
Reference: Parameter Binding in ASP.NET Web API
Reference: Routing in ASP.NET Web API

There are 2 different solutions for this problem.
First:
Send word and userId as QueryStrings.. here's an example:
http://localhost/api/identification?word=hey&&userId=12
this will work in your case so you won't need to change anything in the code.
And Finally my favorite solution:
Create a model class like that:
public class IdentificationModel{
public string word { get; set; }
public string userId { get; set; }
}
then require it as a parameter object in you method like that:
[HttpPost]
public Probabilities Post(IdentificationModel requestModel) {
Request request = new Request();
request.Identify(requestModel.word, requestModel.userId);
Probabilities probabilities = probabilitiesFactory.GetBy(request.ProbabilitiesId, "Id");
return probabilities;
}
The last one does not require adding [FromBody] attribute since any class object is an api method will automatically be waited as a Request Body Object.

Related

Using a controller with WEB API Routing

In my project (Asp.net MVC), I want to use DevExtreme GridView to display my data. I've used code first to create databases and tables. In the project, I have a model with the name of Member. I did right click on the Controller folder and select Add->Controller->DevExtreme Web API Controller with actions, using Entity Framework. In the wizard, I selected my database context and model and determine my controller name (MembersController) and then clicked Add. So in the Views folder, I created a folder with name Members and inside it, I added a view with name Index. (I don't know what exactly name must be for view, you suppose Index). In the index view, I used the wizard to add a DevExtreme GridView (Right-click on the view context and click on Insert A DevExtreme Control Here. In the wizard, I selected GridView as control and DatabaseContext, Member model and Members controller. You can see all of my codes in the below:
Member Mode:
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication2.Models
{
public class Member
{
#region Ctor
public Member()
{
}
#endregion
#region Properties
[Key]
public int MemberID { get; set; }
[Required(ErrorMessage ="*")]
public string FirstName { get; set; }
[Required(ErrorMessage = "*")]
public string LastName { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
[Required(ErrorMessage = "*")]
public string NID { get; set; }
[Required(ErrorMessage = "*")]
public string MID { get; set; }
[Required(ErrorMessage = "*")]
public string SalaryID { get; set; }
#endregion
}
}
Controller:
[Route("api/Members/{action}", Name = "MembersApi")]
public class MembersController : ApiController
{
private ApplicationDbContext _context = new ApplicationDbContext();
[HttpGet]
public HttpResponseMessage Get(DataSourceLoadOptions loadOptions) {
var members = _context.Members.Select(i => new {
i.MemberID,
i.FirstName,
i.LastName,
i.Phone,
i.Mobile,
i.NID,
i.MID,
i.SalaryID
});
return Request.CreateResponse(DataSourceLoader.Load(members, loadOptions));
}
[HttpPost]
public HttpResponseMessage Post(FormDataCollection form) {
var model = new Member();
var values = JsonConvert.DeserializeObject<IDictionary>(form.Get("values"));
PopulateModel(model, values);
Validate(model);
if (!ModelState.IsValid)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, GetFullErrorMessage(ModelState));
var result = _context.Members.Add(model);
_context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Created, result.MemberID);
}
[HttpPut]
public HttpResponseMessage Put(FormDataCollection form) {
var key = Convert.ToInt32(form.Get("key"));
var model = _context.Members.FirstOrDefault(item => item.MemberID == key);
if(model == null)
return Request.CreateResponse(HttpStatusCode.Conflict, "Member not found");
var values = JsonConvert.DeserializeObject<IDictionary>(form.Get("values"));
PopulateModel(model, values);
Validate(model);
if (!ModelState.IsValid)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, GetFullErrorMessage(ModelState));
_context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpDelete]
public void Delete(FormDataCollection form) {
var key = Convert.ToInt32(form.Get("key"));
var model = _context.Members.FirstOrDefault(item => item.MemberID == key);
_context.Members.Remove(model);
_context.SaveChanges();
}
private void PopulateModel(Member model, IDictionary values) {
string MEMBER_ID = nameof(Member.MemberID);
string FIRST_NAME = nameof(Member.FirstName);
string LAST_NAME = nameof(Member.LastName);
string PHONE = nameof(Member.Phone);
string MOBILE = nameof(Member.Mobile);
string NID = nameof(Member.NID);
string MID = nameof(Member.MID);
string SALARY_ID = nameof(Member.SalaryID);
if(values.Contains(MEMBER_ID)) {
model.MemberID = Convert.ToInt32(values[MEMBER_ID]);
}
if(values.Contains(FIRST_NAME)) {
model.FirstName = Convert.ToString(values[FIRST_NAME]);
}
if(values.Contains(LAST_NAME)) {
model.LastName = Convert.ToString(values[LAST_NAME]);
}
if(values.Contains(PHONE)) {
model.Phone = Convert.ToString(values[PHONE]);
}
if(values.Contains(MOBILE)) {
model.Mobile = Convert.ToString(values[MOBILE]);
}
if(values.Contains(NID)) {
model.NID = Convert.ToString(values[NID]);
}
if(values.Contains(MID)) {
model.MID = Convert.ToString(values[MID]);
}
if(values.Contains(SALARY_ID)) {
model.SalaryID = Convert.ToString(values[SALARY_ID]);
}
}
private string GetFullErrorMessage(ModelStateDictionary modelState) {
var messages = new List<string>();
foreach(var entry in modelState) {
foreach(var error in entry.Value.Errors)
messages.Add(error.ErrorMessage);
}
return String.Join(" ", messages);
}
protected override void Dispose(bool disposing) {
if (disposing) {
_context.Dispose();
}
base.Dispose(disposing);
}
}
View:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#(Html.DevExtreme().DataGrid<WebApplication2.Models.Member>()
.DataSource(ds => ds.WebApi()
.RouteName("MembersApi")
.LoadAction("Get")
.InsertAction("Post")
.UpdateAction("Put")
.DeleteAction("Delete")
.Key("MemberID")
)
.RemoteOperations(true)
.Columns(columns => {
columns.AddFor(m => m.MemberID);
columns.AddFor(m => m.FirstName);
columns.AddFor(m => m.LastName);
columns.AddFor(m => m.Phone);
columns.AddFor(m => m.Mobile);
columns.AddFor(m => m.NID);
columns.AddFor(m => m.MID);
columns.AddFor(m => m.SalaryID);
})
.Editing(e => e
.AllowAdding(true)
.AllowUpdating(true)
.AllowDeleting(true)
)
)
WebApiConfig.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebApplication2
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// WebAPI when dealing with JSON & JavaScript!
// Setup json serialization to serialize classes to camel (std. Json format)
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}
}
}
Global.asax.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace WebApplication2
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
In addition I've installed all requirements for this project according this link.
But when I try to show View with https://localhost:44328/Members/index RUL, I get this error:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Members/index
I'v tried a lot way to correct my wrong but I couldn't find solution. I almost read all of documents about routing (mvc and web api), but after about 5 days I still couldn't to solve it.
Thanks a lot for answer me.
The thing is as far as I can tell, one of the reasons you are receiving a 404 is because you don't seem to be adding your parameter anywhere. Aside from that your 'DataSourceLoadOptions loadOptions' shouldn't be used as a parameter because it is probably too complex. Shouldn't you create a service which retrieves your loadOptions instead of you giving it along?
If you want all members without giving information then you should do exactly that. Not give the request some metadata it doesn't know about along for the ride.
I suggest you do the following:
Create an API which does not need metadata like how to get a datasource. Things such as Members.LastName are acceptable
Make sure you create a service which is responsible for getting your data in the first place. This means also removing all that extra code in your controller and placing it in a more suitable location.
Keep your classes clean and simple. Your controller now has too many responsibilities.
Hopefully this'll help. If you try your API GET Method as is without the 'DataSourceLoadOptions loadOptions' parameter, then your API will not return 404.
Since you didn't put in your ajax call url, I'm going to have to work with this
Requested URL: /Members/index
This is a problem, your webApi default route requires your URL to be prepended with /api/
So something like this should work /api/Members, so you can remove the Index part of that URL as the request type will handle which Action is executed ie HTTPGet/HTTPPost
EDIT: Use this as your route
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Members" id = RouteParameter.Optional }
);

OData WebAPI 2.2 Post method Example

I am in creation of webapi odata service. I have a post method named "Validate" in my controller named "User" as below:
public class UserController : ODataController
{
[HttpPost]
public HttpResponseMessage Post(LoginEntityDto objLogin)
{
//mylogin
}
}
//Custom class
public class LoginEntityDto
{
public string username { get; set; }
public string password { get; set; }
}
I am inputting this post method with the below JSON Format
{
"username": "C201566",
"password" : "pwd"
}
I am using the content-type : application / json
but in my post method the input objLogin is always null
Please help me in resolving this issue.
OData/Webapi doesn't support post a complex type in this way, why not make it as an entity type by define the key and entityset.
[key]
username {get;set;}
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<LoginEntityDto>("LoginEntityDto");
or you can use action to post this complex type as parameter.
FYI: http://odata.github.io/WebApi/#04-07-action-parameter-support

Server Error in '/' Application in ActionLink (but submit works fine)

Execution of the following lines
#Html.ActionLink("Open",actionName: "DownloadExcelFile",
controllerName: "Excel",
routeValues: new { model = new ExcelModel(5, "JobName", "item.UserName") },
htmlAttributes: null)
returns Server Error in '/' Application, could you help me to fix them?
Note that when I change the name of the parameter, model -> id, I get an Error 404 instead of Server Error in '/' Application.
The model is
public class ExcelModel
{
public int InputA { get; set; }
public string InputB { get; set; }
public string UserName { get; set; }
public ExcelModel(int inputA, string inputB, string userName)
{
InputA = inputA;
InputB = inputB;
UserName = userName;
}
public ExcelModel()
{
InputA = 1;
InputB = "B1";
UserName = "NiceUser";
}
...
}
Controller is
public class ExcelController : Controller
{
public ActionResult Index()
{
var model = new ExcelModel(1, "B1", User.Identity.Name);
return View(model);
}
[HttpPost]
public ActionResult DownloadExcelFile(ExcelModel id)
{
// assume we create an an excel stream...
MemoryStream excelStream = id.BuildSpreadsheet();
return new FileStreamResult(excelStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "myfile.xslx"
};
}
}
RouteConfig is the standard one
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Finally, as mentioned earlier, the method itself is fine, since it works perfectly with submit, as below:
#using (Html.BeginForm("DownloadExcelFile", "Excel"))
{
<fieldset>
// fields names and values
<p>
<input type="submit" value="Open Excel"/>
</p>
</fieldset>
}
1) You can't pass an entire class as a route value param. The helper has to be able to put whatever you pass into a URL, which means it has to be something that can be converted to a string value. It might be possible to JSON encode the model and then pass the JSON string for the param, but the helper isn't going to make such assumptions for you, nor would it necessarily know how to JSON encode it for you, if it did.
2) When you just pass the id, you get a 404 because your action doesn't not accept an int for id, but rather expects ExcelModel, which as we discussed in #1, is not possible to pass via URL.
Your controller method is marked with the HttpPost attribute. This means that it only accepts POST-requests and not GET-requests. Normal link visits are GET-requests, so that is probably the problem. (Read more here)
Remove the HttpPost attribute and see if that fixes the problem.

web api 2.1 routing not working but same works on mvc

I'm sending multiple requests to the same action method in a controller all these requests have some common querystring attributes and some specific to that request.
request1 : http://localhost/home/test?a=a1&b=b1&c=c1&d=d1....around 25 parameters
request2 : http://localhost/home/test?a=a1&b=b1&j=j1&k=k1...around 20 parameters
similarly request 3 , request4,etc...
My action method in mvc in homecontroller is as below..
public string test(string a, string b, string c, string d, ...around 50 parameters)
This is working perfectly..
But when I take this code and move it to web api, it no longer works..
Moreover, if I try with just two parameters, it works and I can get the two parameters..
public string test(string a, string b)
I have no control on the requests that I receive in my application as it is coming from a 3rd party host application, so the method name and parameters can not change ...
The route configured in mvc in route.config is standard..
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have configured a separate route for webapi in webapiconfig on similar lines..
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Any ideas how to solve this..
Thanks
Arnab
The reason is that Web API does action overloading and all these parameters are required, if they are not provided you are ending up with 404. The simple answer to your question is to make them optional by giving them a default value, so your signature will look like this:
public IHttpActionResult Get(string a = null, string b = null, ...)
However this code seems very elaborate for what you are doing, it's probably also not the most efficient and you end up with a lot of if statements.
Consider alternatively just parsing the query string yourself and get a more convenient to use data set.
public class ValuesController : ApiController
{
public IHttpActionResult Get()
{
var collection = Request.RequestUri.ParseQueryString();
foreach (var key in collection.Keys)
{
var value = collection[(string)key];
// do something with key & value
}
return Ok();
}
}
and as another option is to build a model including all the parameters, something like:
public class Settings
{
public string A { get; set; }
public string B { get; set; }
...
}
and bind to the model using the FromUri:
public IHttpActionResult Get([FromUri]Settings settings)
{
...
}
Here is a link from Mike Stall's blog - http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

Web Api GET method that has a nullable Guid possible?

I have a MVC Web API get method that I'd like to be able to pass a nullable Guid as a parameter. If I setup the GET with a "?Id=null" I get a 400 response. I can pass a empty guid but that I'd rather not do that.
No matter what I change the URI to, "id=, id=null etc" it won't accept null. Does anyone know how to make this work?
[HttpGet]
public User Get(Guid? Id)
Update Route config
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Full Http Get signature, sourceId is the param that id like to pass as null.
[HttpGet]
public IEnumerable<ActionItemsListViewModel> GetPagedList(int skip, int take, int page, int pageSize, [FromUri]List<GridSortInfo> sort, [FromUri] ActionItem.ActionItemStatusTypes? actionItemStatus, Guid? sourceId)
Found the problem, this filter was saying the ModelState was invalid.
public class ApiValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid )
{
var errors = actionContext.ModelState
.Where(e => e.Value.Errors.Count > 0)
.Select(e => e.Value.Errors.First().ErrorMessage).ToList();
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, string.Join(" ", errors));
}
}
}
Try to use:
[HttpGet]
public User Get(Guid? Id = null)
I was able to pass null to the Guid? when I use
query string parameter: api/values?id=null
route parameter: api/values/null
Controller:
public class ValuesController : ApiController
{
public User Get(Guid? Id)
{ ... }
}

Resources