There are many ways to pass parameters to a controller from a view.
Method One (recommended):
Routing
Config. Routes.maphttproute (Name: "Defaultapi", Routetemplate: "Api/{controller}/{action}", defaults:new {id = Routeparameter.optional});
Controller
Public string Get () {return"Web Api,get by ID and value";} Public string Get (int ID,string value) {return'Web Api,get by ID and Value";}
View
$.ajax ({type:'Get', URL:'Api/values/get', Success:function (data) {alert (data); }, Error:function () {Setcontainer ('error!'); }}); $.ajax ({type:'Get', URL:'api/values/get?id=12&value=1234', Success:function (data) {alert (data); }, Error:function () {Setcontainer ('error!'); }});
Pros: Parameters are written directly in the URL without having to customize multiple routing rules, and a routing rule can implement requests in multiple formats
Note: The parameters of the post request in the Web API are modified with [frombody] or [Fromuri], and this method cannot be used to pass arguments
, because the rules of [frombody] are not the same as normal, not the key=value format, directly =value
Routing rules do not have to be modified
Controller POST method
Public string Post ([frombody]string value) { return"Web Api,post by value" ;}
View
$.ajax ({type:'Post', URL:'Api/values/post', DataType:'JSON',//when data is in JSON format, this sentence must be addedData: {"":"Meterid"}, ContentType:'Application/json', Success:function (data) {alert (data); }, Error:function () {Setcontainer ('error!'); }});
However, data-based communication is best limited to post requests because errors in put and delete are not known to be possible.
There is also a better solution is to use [Fromuri] to modify the Post method parameters, this time the parameters can be passed and normal parameters
is delivered in the same way
Method two (not recommended):
CONFIG. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{action}", defaults:New{id =routeparameter.optional}); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{action}/{id}/{value}", defaults:New{id =routeparameter.optional});
Controller
Public string Get () {return"Web Api,get by ID and value";} Public string Get (int ID,string value) {return'Web Api,get by ID and Value";}
View
$.ajax ({type:'Get', URL:'Api/values/get', Success:function (data) {alert (data); }, Error:function () {Setcontainer ('error!'); }}); $.ajax ({type:'Get', URL:'api/values/get/id=12/value=1234', Success:function (data) {alert (data); }, Error:function () {Setcontainer ('error!'); }});
Disadvantage: This way to the Controller in accordance with the parameters of the method, make different routing rules, and then write the corresponding URL, with the method
The difference is that the routing rules and the way URLs are written,
Other:
That's what we said. Data is not recommended because the put and delete errors, and when the data format is different, it may also
To set formats such as DataType and contenttype
MVC Beginner Controller parameter Transfer impressions