C# call webservice by url
I'm working on a webapplication and i want to call a webservice through it's url. How can i achieve this?
I've got the following route registered in my global.asax;
routes.MapRoute(
"ServiceHandler",
"{*path}/{*.svc}",
new { Url = "Services/Service.svc/GetLatestTweets" }
);
And i want to call the webservice, in this case to get the latest messages from twitter by an url similar to the one below;
http://www.domainname.com/Services/Service.svc/GetLatestTweets
When i use the registered route as mentioned i'm getting an error with the stackstrace i added bel开发者_JAVA技巧ow.
[ArgumentException: A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
System.Web.Routing.RouteParser.Parse(String routeUrl) +3823656
System.Web.Routing.Route..ctor(String url, IRouteHandler routeHandler) +24
System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +86
System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints) +28
System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults) +18
CMS.Presentation.FrontEnd.Framework.CMSApplication.RegisterRoutes(RouteCollection routes) in C:\Projects\Website\Web.Presentation\FrontEnd\Framework\CMSApplication.cs:62
CMS.Presentation.FrontEnd.Framework.CMSApplication.Application_Start() in C:\Projects\Website\Web.Presentation\FrontEnd\Framework\CMSApplication.cs:80
[HttpException (0x80004005): A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +3988565
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +325
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375
[HttpException (0x80004005): A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11529072
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4784373
What should i do or change so i can call the webservice by it's url? I'm working with a simple wcf-service
You cannot achieve this. The .svc
extension is already registered with a very different handler than MVC in order to process WCF. If you try to change it the service will never work. You might need to call the web service from your controller action which will act as bridge.
Maybe I am missing something, but it seems like you could just
routes.MapRoute(
"ServiceHandler",
"Services/{service}.svc/{*path}",
new { controller = "services", action = "ParseAndExecuteServiceCall" service = "TwitterService", path = "GetLatestTweets" }
);
and parse the remote call in the controller, if it is more complex. You can only have one "catchall" parameter in routes, but you can use as many variables as you want, and restrict/define them with IRouteConstraint implementations or Regex patterns.
Also, with response to the comment below about the .svc vs. .mvc handler - There is nothing to limit you from overriding the IHttpHandler In order to do this, you would need to override the IRouteHandler in's method for GetHttpHandler()... Then you can do whatever processing you want.
精彩评论