【问题标题】:Configuration .net core web application and webapi on IIS在 IIS 上配置 .net core web 应用程序和 webapi
【发布时间】:2019-05-29 05:19:14
【问题描述】:

我正在创建我的 Web 应用程序 (WebUI) 和 webapi (WebApi)。在 ASP.Net 中,我一直以这种方式在 IIS 上创建带有结果调试文件夹路径的 Web 应用程序,并将 WebApi 作为 Web 端内的 Web 应用程序。在 Visual Studio 附加过程 w3wp.exe 中,我可以调试代码。我现在也想达到同样的效果。

现在我有了 WebUI(Web 应用程序 .net Core 2.2)和 WebApi(Web api .net Core 2.2)。在 IIS 中添加新站点(WebUi Debug 文件夹的路径)并在内部创建应用程序(WebApi Debug 文件夹的路径)

我的路线

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

想要的效果:

http://localhost:8100/ - 网站

http://localhost:8100/webapi/api/v1/values - 端点 webapi

网站可以工作,但 WebApi 不行。我需要同时使用站点和 api。当我在 Visual Studio 中运行 Web Api 时,Web Api 工作正常。同时调试的任何建议或更好的想法?当 iis 中的应用程序 web api 导航到发布 webapi 时,一切正常。

【问题讨论】:

  • 我发现为网站和 API 使用不同的端口号更容易。我确定您尝试做的事情是可以实现的,但以前从未见过为 dotnet core api 项目做过。也不值得 IMO。
  • IIS 路径应该导航到 ..\..\WebAPI\bin\Debug\netcoreapp2.2 ?现在站点返回 503 错误,api 返回 404.0 错误。所有路径导航到 netcoreapp2.2(站点和 api 不同)我应该更改配置解决方案或 RouteConfig.cs 中的任何内容? - 以前我使用默认设置
  • 你试过下面的答案了吗?
  • 很遗憾您的解决方案不起作用。在我的解决方案 Api 中,我没有 WebApiConfig。只有 Startup.cs

标签: c# iis .net-core asp.net-core-webapi


【解决方案1】:

在 RouteConfig.cs 中试试这个。

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

对于 WebAPiConfig.cs 中的 api 路由

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

另一方面,您可以像这样直接在 Api Controller 中定义路由。

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

祝你好运。

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 2020-06-12
    • 2019-01-05
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    相关资源
    最近更新 更多