MVC Beginner Controller parameter Transfer impressions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.