简体   繁体   English

同一网站中的ASP.NET WebAPI和Razor页面

[英]ASP.NET WebAPI and Razor Pages in the same web location

I've developed a project that consisted of just a WebApi, which was deployed through VS WebDeploy at the root of the webspace. 我已经开发了一个仅由WebApi组成的项目,该项目是通过VS WebDeploy在Webspace的根部进行部署的。 With new requirements, I also needed to host a webapp in the same webspace (Razor pages). 有了新的要求,我还需要在同一个Web空间(Razor页面)中托管一个Web应用程序。 I decided to keep the projects separated in my Visual Studio Solution, and publish the API into the {root}/webapi/ folder, and the Razor webapp in the {root} folder. 我决定在Visual Studio解决方案中将项目分开,然后将API发布到{root}/webapi/文件夹中,并将Razor webapp发布到{root}文件夹中。

I started by republishing the webapi into the new folder location and making a test API call (adding the folder location as part of the url, so http:/example.com/ webapi /route), but the Server returned Server Error in '/' Application. Runtime Error 我首先将webapi重新发布到新的文件夹位置,然后进行测试API调用(将文件夹位置添加为url的一部分,因此添加了http:/example.com/ webapi / route),但是Server Error in '/' Application. Runtime Error返回了Server Error in '/' Application. Runtime Error Server Error in '/' Application. Runtime Error . Server Error in '/' Application. Runtime Error

I wonder if there's some other parameter that needs to be changed when moving the project to a subfolder, and any other solution to the problem I'm trying to tackle is welcome. 我想知道在将项目移到子文件夹时是否需要更改其他参数,欢迎您尝试解决此问题的任何其他解决方案。

PS: I don't have access to the IIS Server settings of the hosting PS:我无权访问主机的IIS服务器设置

I'm not an expert at hosting .NET Core apps on IIS but chances are that you can't make this setup work if you're not allowed to change the IIS Server configuration. 我不是在IIS上托管.NET Core应用程序的专家,但是如果您不允许更改IIS服务器配置,则很有可能无法使此安装程序起作用。 I assume you need to add your nested API project as a Website to make IIS actually run it. 我假设您需要将嵌套的API项目添加为网站,以使IIS实际运行它。

So, it looks like you need some kind of multi-tenancy. 因此,看起来您需要某种多租户。 As your question is about Razor Pages, I presume we are dealing with ASP.NET Core here instead of classic ASP.NET (suggested by your tags). 因为您的问题是关于Razor Pages的,所以我想我们在这里使用的是ASP.NET Core,而不是经典的ASP.NET(由您的标签建议)。

In ASP.NET Core it's pretty easy to setup multi-tenant applications using application branches . 在ASP.NET Core中,使用应用程序分支设置多租户应用程序非常容易。 In this case your Startup.Configure method of your main Razor Pages project would look like this: 在这种情况下,主Razor Pages项目的Startup.Configure方法将如下所示:

public void Configure(IApplicationBuilder app)
{
    app.Map("/webapi", ConfigureWebApiBranch);

    // configuration of your Razor Pages app pipeline...

    app.UseMvc(...);
}

public void ConfigureWebApiBranch(IApplicationBuilder app)
{
    // configuration of your Web Api app pipeline...

    app.UseMvc(...);
}

Of course, ConfigureWebApiBranch can reside in another assembly so you can keep the two projects logically separated. 当然, ConfigureWebApiBranch可以驻留在另一个程序集中,因此您可以使两个项目在逻辑上保持分离。

There's one gotcha regarding this solution. 关于此解决方案有一个陷阱。 Both the main and the web api branch shares the same DI container configured in Startup.ConfigureServices of your main project. 主分支和Web api分支都共享在主项目的Startup.ConfigureServices中配置的相同DI容器。 But this can be fixed, as well, if necessary. 但是,如有必要,也可以解决此问题。 You find a detailed explanation of this eg here . 您可以在此处找到对此的详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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