Solution for hosting the WCF Rest service in ASP. NET MVC

Source: Internet
Author: User

Here, we use the parameters in routes. MapRoute (parameter) for processing. The MapRoute overload is as follows:

RouteTable.Routes.MapRoute( string name, string url);RouteTable.Routes.MapRoute( string name, string url, object defaults);RouteTable.Routes.MapRoute( string name, string url, string[] namespaces);RouteTable.Routes.MapRoute( string name, string url, object defaults, object constraints);RouteTable.Routes.MapRoute( string name, string url, object defaults, string[] namespaces);RouteTable.Routes.MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

 

Name parameter: the rule name can be retrieved at will, but cannot be duplicated. Otherwise, an error is returned. There is already a "default" route in the route set. The route name must be unique.

Url parameter: the rule for the url to obtain data. This is not a regular expression. You can include the parameters to be identified, for example, {controller}/{action}

Note: at least one Routing rule can be created by passing the name and url parameters. For example, the instance can be changed to: RouteTable. Routes. MapRoute ("Default", "{controller}/{action }");,

Defaults parameter: the default value in the url parameter. If a URL only has controller: localhost/home/, but we only create one url to obtain data rules: {controller}/{action }, in this case, the default value specified in the defaults parameter is set for the action parameter. ults is of the Object type, and an anonymous type can be passed to initialize the default value. (Action, that is, the index call method Userdetail ())

New {controller = "Home", action = "Userdetail "});

Constraints parameter: used to limit the rule of each parameter or the type of Http request. The constraints attribute is a RouteValueDictionary object, which is represented by a dictionary. The dictionary value can be, A string used to define a regular expression. The regular expression is case-insensitive. B. The object that implements the IRouteConstraint interface and contains the Match method. The regular expression can be used to specify the parameter format. For example, the controller parameter can only contain four digits: new {controller = @ "\ d {4 }"}

Public static void RegisterRoutes (RouteCollection routes) {// ignore the pair. the Route of the axd file, that is, directly accessing the file like WebForm. axd file routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. ignoreRoute ("{resource }. ashx/{* pathInfo} "); routes. mapRoute ("Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home ", action = "Index", id = UrlParameter. optional}, // Parameter defaults new { Controller = @ "^ \ B (?! GetRegSourceData) \ w * \ B $ "}); routes. Add (new ServiceRoute (" GetRegSourceData ", new WebServiceHostFactory (), typeof (GetRegSourceData )));}

View the code above, where new {controller = @ "^ \ B (?! GetRegSourceData) \ w * \ B $ "} filters out the URI whose Controller is GetRegSourceData.

Then match the Route of the WCF Rest Service.

routes.Add(new ServiceRoute("GetRegSourceData", new WebServiceHostFactory(), typeof(GetRegSourceData)));

 

Related Article

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.