Problems and Solutions for hosting the WCF Rest service in ASP. NET MVC

Source: Internet
Author: User
Tags actionlink

I recently studied ASP. net mvc 3 and the WCF Reset Service. I suddenly wanted to see what would happen if I put them together. Start from here. The code I wrote is as follows:

Public class MvcApplication: System. Web. HttpApplication

{

Public static void RegisterGlobalFilters (GlobalFilterCollection filters)

{

Filters. Add (new HandleErrorAttribute ());

}

 

Public static void RegisterRoutes (RouteCollection routes)

{

Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");

 

Routes. MapRoute (

"Default", // Route name

"{Controller}/{action}/{id}", // URL with parameters

New {controller = "Home", action = "Index", id = UrlParameter. Optional}

);

 

}

 

Protected void Application_Start ()

{

AreaRegistration. RegisterAllAreas ();

RegisterGlobalFilters (GlobalFilters. Filters );

RouteTable. Routes. Add (new ServiceRoute ("Uap", new WebServiceHostFactory (), typeof (Service. UapService )));

RegisterRoutes (RouteTable. Routes );

}

}

Then I rushed to press F5. well, the homepage was as expected, and the Service Url was also normal. It seems everything is normal. Excited. Click a connection. Then the cup appears.

Http: // localhost: 3315/Uap? Action = About & controller = Home ...... This ...... What is this address? I have never seen it. Sorry.

......

............

I haven't figured out what's going on for a long time.

The check code cannot solve the problem either.


<Ul id = "menu">

<Li> @ Html. ActionLink ("Home", "Index", "Home") </li>

<Li> @ Html. ActionLink ("Application", "Index", "Application") </li>

<Li> @ Html. ActionLink ("About", "About", "Home") </li>

</Ul>

I swear that the code works normally before joining the WCF Rest Service.

It seems that

RouteTable. Routes. Add (new ServiceRoute ("Uap", new WebServiceHostFactory (), typeof (Service. UapService )));

This line of code is a ghost.

I put this line of code in

RegisterRoutes (RouteTable. Routes );

The following shows that the connected URL is normal, and the WCF Rest Service cannot work again. It's really amazing.

After all, I downloaded the source code of MVC.

After a burst of f5. F11, I finally found the problem.

The original ActionLink method polls all Route when generating a Url. It requires that the qualified Route generate the Url with the specified parameter and return the first generated value, as a result, ServiceRoute does not know whether the head burned out or why. Returned/Uap? Action = About & controller = Home. The ActionLink method is output to the page in the original mode. (You can't see if it is fooled. It's a silly guy ).

It seems that the crux of the problem has been found. Now we should try to solve the problem.

The most direct method is to modify the source code of MVC. However, after reading the source code of the entire MVC project, I gave up this crazy idea.

However, a method is found, that is

RouteTable. Routes. Add (new ServiceRoute ("Uap", new WebServiceHostFactory (), typeof (Service. UapService )));

All connected URLs will be normal after being placed in all MVC Route.

However, the Route address of the WCF Rest Service will be

Routes. MapRoute (

"Default", // Route name

"{Controller}/{action}/{id}", // URL with parameters

New {controller = "Home", action = "Index", id = UrlParameter. Optional}

Intercept.

Headaches ......

It seems that you have to try to make the MVC Route ignore the Route address of ServiceRoute. What should we do.

Ignore? Yes, ignore!

Just do it.

Routes. IgnoreRoute ("/Uap/{* pathInfo }");

This should work, right?

Press F5 and the homepage is normal. The link Url is normal. the Wcf Service? Abnormal. Rely on ......

Take a closer look, rely on ......

Headache, The IgnoreRoute and MVC have no relationship with half a cent.

Routes. IgnoreRoute ("/Uap/{* pathInfo }");

Simply block the Wcf Service.

It seems that this still does not work. Do you really want to modify the source code of MVC?

I don't want to do this. I want to study it again. I would rather stay for half an hour than write a line of code. After all, I feel a sense of accomplishment. (A cool who should be despised spent half an hour trying to write less code .)

When I started MSDN, I suddenly remembered that the MapRoute method had a parameter for specifying a routing rule. This parameter can be passed with a regular expression to constrain routing rules.

It seems that I have found the golden key to solve the problem.

Write the following code:


Routes. MapRoute (


"Default", // Route name

"{Controller}/{action}/{id}", // URL with parameters

New {controller = "Home", action = "Index", id = UrlParameter. Optional}, // Parameter defaults

New {controller = @ "^ (?! Uap) w * $ "}

The rule is simple. Ignore all paths with uap characters.

After running, I found everything was normal and finally solved the problem.

Paste the source code:


Public class MvcApplication: System. Web. HttpApplication

{


Public static void RegisterGlobalFilters (GlobalFilterCollection filters)


{


Filters. Add (new HandleErrorAttribute ());


}

 


Public static void RegisterRoutes (RouteCollection routes)


{


Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");

 


Routes. MapRoute (


"Default", // Route name

"{Controller}/{action}/{id}", // URL with parameters

New {controller = "Home", action = "Index", id = UrlParameter. Optional}, // Parameter defaults

New {controller = @ "^ (?! Uap) w * $ "}


);

 


}

 


Protected void Application_Start ()


{


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.