MVC web API determines whether it is a local environment

Source: Internet
Author: User

Sometimes write locallyCodeWhich has many judgments. Or what is the direct transfer of parameters?

You need to use the local environment. If it is not a local environment, access is normal. In the local environment, data is directly returned.

In the past, 127.0.0.1 or others were directly judged.

Now:

Httprequest. islocal can be implemented

Whether it is localhost or IIS 127.0.0.3ip access. Whether to directly bind the host to the domain name asd.com for access can be determined normally

First, add an extension method.

  Public   Static   Class  Httprequestmessageextensions {  Public   Static   Bool Islocal ( This Httprequestmessage request ){  VaR Localflag = request. properties [ "  Ms_islocal  " ] As Lazy < Bool > ;  Return Localflag! = Null && Localflag. Value ;}} 

Use

  //  Get API/Values         Public Ienumerable < String > Get (){  If (! Request. islocal ()){  Return   New   String [] { "  Value1  " , "  Value2  " };}  Else  {  Return   New   String [] { "  Value1  "  };}} 

 

Very convenient.

 

 

 

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.