简体   繁体   中英

Configuration .net core web application and webapi on IIS

I'm in progress a create my web application (WebUI) and webapi (WebApi). In ASP.Net I have always worked that way create web application on IIS with path to result debug folder, and WebApi as web application inside webside. In Visual Studio attach process w3wp.exe and I could debug the code. I would like to achieve the same effect now.

Now I have WebUI (Web application .net Core 2.2) and WebApi (web api .net Core 2.2). In IIS add new Site (path to WebUi Debug folder) and inside create application (path to WebApi Debug folder)

my routing

[Route("api/v1/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }

Desired effect:

http://localhost:8100/ - website

http://localhost:8100/webapi/api/v1/values - endpoint webapi

website it works but WebApi not. I need to use site and api at the same time. When I run Web Api in Visual Studio web Api work correct. Any advice or a better idea for debug in the same time? Everything work fine when application web api in iis navigate to publish webapi.

Try this in RouteConfig.cs.

routes.MapRoute(
    name: "Home",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
);

And for api routes in WebAPiConfig.cs

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

On the other hand you can define route directly in Api Controller like this.

[ApiController]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    [Route("webapi/api/v1/values")]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

Good luck.

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