. Net Core 2.0 Learning Routing and request parameter passing

Source: Internet
Author: User

First, configure the default routing method

{Controller=home}/{action=index}/{id?}

Default Request Address: Http://localhost:xxx/home/index

/ID? is an option such as

In HomeController

         Public actionresult Index ()        {            return Content ("OK");        }          Public ActionResult Index (int  ID)        {            return Content ("OK " );        }    

The first method is the one that the default route points to.

The second method, the original request address should be: http://localhost:xxx/home/index?id=1, because/id, for the sake of can be changed to: HTTP://LOCALHOST:XXX/HOME/INDEX/1

Second, request parameter pass

1. Receive request parameters in the form of an object such as:

We have a class for receiving parameters

  

The corresponding method is as follows, plus [Frombody] 's purpose is to tell it to get the parameters in the body

 Public class paramses{    publicstringgetset;}      Public string Get Set ; }      Public int Get Set ; }}

The parameter can be followed at the end of the address, or it can be an ajax JSON parameter such as:

Http://localhost:xxx/home/text?id=asdqwe&name=haos&age=1

$.ajax ({url:"", type:"Post",data:{ID:"qwe", Name:"ASD", age:1< c5>}})

2. Receive with parameter parameters of the object class

At this time the parameters are more flexible, can be arbitrary string;

Like what:

$.ajax ({url:"", type:"Post",data:{ID:"qwe", Name:"ASD", Age: 1 }})

What is received at this time

Then use the following method to convert to a dictionary to get the corresponding content

dictionary<stringstring> P = jsonconvert.deserializeobject<dictionary<string  string>>(paramses. ToString ()); var i = p["ID"]; var int. Parse (p["Age"]);

. Net Core 2.0 Learning Routing and request parameter passing

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.