简体   繁体   中英

Owin Self Hosted Service Map .svc to web api route (ignore .svc in a url)

I have an existing Self-Hosted REST service that uses System.ServiceModel.ServiceHost (see the example at the bottom of this page: https://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx )

I have this running on an address similar to this httpX://MyServer:80/SubFolder/Service.svc. The Svc end point was there historically when we used to have SOAP and REST end-points. But it's now only used by clients accessing the REST end-point. I need to keep the Url unchanged to support existing clients

I want to switch this out to an Owin self-hosted site. But I can't figure how to make the routing handle the .svc portion of the URL and basically ignore it.

eg this works httpX://MyServer:80/SubFolder/Service/GetTest

This does not httpX://MyServer:80/SubFolder/Service.svc/GetTest

I've been looking at Url re-write options and found these but they don't appear to address my problem. https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

(sorry - I can't post more examples of what I looked at due to Stackoverflow restrictions)

My startup is like this

var server = WebApp.Start<Startup>("httpX://MyServer:80/SubFolder/Service.svc");

and my routing looks like this

appBuilder.Map("/api", api =>
    {
        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();
        api.UseWebApi(config);
    });

With this setup if I browse to httpX://MyServer:80/SubFolder/Service.svc/api/Test I get a 503.

If I modify to be like this and remove .svc from the address in a browser it works.

var server = WebApp.Start<Startup>("httpX://MyServer:80/SubFolder/Service");

So I finally discovered that my problem was that I had another site running within IIS on the same SubFolder, IIS was using https and my new site is self-hosting on http but that was causing me to have additional problems.

I also found a solution by doing this

[Route("api/[controller].svc")]
public class ServiceController : ApiController
{
    [HttpGet]
    public IHttpActionResult Get()
    {
        return Ok(DateTimeOffset.Now);
    }
}

And then adding these routes

    config.Routes.MapHttpRoute(
        name: "DefaultWcfWithSvcExt",
        routeTemplate: "{controller}.svc/{id}",
        defaults: new { id = RouteParameter.Optional });

    config.Routes.MapHttpRoute(
        name: "DefaultWcf",
        routeTemplate: "{controller}/{id}",
        defaults: new { id = RouteParameter.Optional });

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM