The ASP. NET Web API accepts Angualrjs's querystring in two ways

Source: Internet
Author: User

How does the ASP. NET Web API accept querystring from ANGUALRJS? This article experiences two ways.

The first way: Http://localhost:49705/api/products?search=GDN

This method is QueryString native format.

First, put the current domain name and the port number into a custom module.

1 (function  () {2     "use strict"; 3 4     Angular.module ("CustomModule", ["Ngresource"])5         . Constant ("AppSettings", { 6             Serverpath: "http://localhost:49705/"7        }); 8 } ());

Above, rely on Ngresource This module, this module in the Angular-resource.js file, note the need to reference the file.

Next, create a custom service for CustomModule by factory.

(function  () {    "use strict";     // A service was created in the way of a factory    Angular.module ("CustomModule")        . Factory ("Productresource", ["$resource", "appSettings", Productresouce]);     function productresouce ($resource, appSettings) {        return $resource (Appsettings.serverpath + "/api /products ");}    } ());

Above, Productresource This service returns the full API request path through the $resource package.


Now, a controller needs to use the Productresource service.

(function  () {    "use strict";    Angular        . Module ("Productmanagement")        . Controller ("Productlistctrl",                     Productlistctrl);     function Productlistctrl (productresource) {        varthis;         = "GDN";        Productresource.query ({Search:vm.searchCriteria},function  (data) {            = data;        }) ;    }} ());

Above, from the front came the name of search querystring.

In the backend ASP. NET Web API, the parameter name of the action method must also be search.

 Public Ienumerable<product> Get (string  search) {    varnew  Productrepository ();     var products = productrepository.retrieve ();     return products. Where (p = = P.productcode.contains (search));}

The Webapiconfig class corresponds to the following settings:

CONFIG. Routes.maphttproute (    "defaultapi",    "api/{ Controller}/{id}",    new {id = routeparameter.optional});

At this point, the front end is requesting data in this way Http://localhost:49705/api/products?search=GDN.

What if we want to request data in this way Http://localhost:49705/api/products/GDN?

The second way: Http://localhost:49705/api/products/GDN

This way you need to do some routing settings.

You need to let angularjs know that the value passed in this position after products/is assigned to the querystring of search.

Also let the ASP Web API know that when reading the value behind products/, it is to be assigned to the search variable in the route.

How do I make the ANGULARJS route conform to the format?

function productresouce ($resource, appSettings) {    return $resource (Appsettings.serverpath + "/api/ Products/:search ");}

How do I get the ASP. NET Web API's route to match Angualrjs?

First, the search querystring is allowed in the route.

CONFIG. Routes.maphttproute (    "defaultapi",    "api/{ Controller}/{search}",    new {search = routeparameter.optional});

Then, accept the search QueryString in the action method.

 Public Ienumerable<product> Get (string  search) {    varnew  Productrepository ();     var products = productrepository.retrieve ();     return products. Where (p = = P.productcode.contains (search));}

At this point, the front end is requested in this way Http://localhost:49705/api/products/GDN.


Summarize:

If you want to get querystring by default, just make sure that the query variables in the front and back sections are consistent, like the search here
If you want to get the querystring in a custom format, the front-end ANGUALRJS needs to define the route in a way similar to/api/products/:search, and the backend ASP also needs to use search as a variable in the route , in CONFIG. Set in the Routes.maphttproute method

The ASP. NET Web API accepts Angualrjs's querystring in two ways

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.