[英]Setting project url in VS2015 ASP.NET 5 Web API application
I'm trying to create a Web API project and a client-side web project, where the web project can access the API via ajax.我正在尝试创建一个 Web API 项目和一个客户端 Web 项目,其中 Web 项目可以通过 ajax 访问 API。 Currently my project looks like this:
目前我的项目是这样的:
I saw this answer on here: Setting app a separate Web API project and ASP.NET app , which explains how the project url can be set to localhost:[port]/api .我在这里看到了这个答案: 将应用程序设置为单独的 Web API 项目和 ASP.NET 应用程序,它解释了如何将项目 url 设置为localhost:[port]/api 。
But for ASP.NET 5 projects, the properties only have 3 tabs (as opposed to the several found in ASP.NET 4 projects):但是对于 ASP.NET 5 项目,属性只有 3 个选项卡(与 ASP.NET 4 项目中的几个选项卡相反):
What I'm wondering is:我想知道的是:
Any help would be appreciated!任何帮助,将不胜感激!
First Point:第一点:
Generally speaking in ASP.NET 5, the routing defaults are very good and should work out of the box without much in the way of configuration.一般来说,在 ASP.NET 5 中,路由默认值非常好,应该开箱即用,无需太多配置。 You can use configuration and/or attribute based routing in your application (with a detailed overview of both here ), although my personal preference is for the attributed approach.
您可以在您的应用程序中使用配置和/或基于属性的路由( 此处详细概述了两者),尽管我个人更喜欢属性方法。 Provided you have the following line in your Startup.cs file (which you should have in a new project):
假设您的 Startup.cs 文件中有以下行(您应该在新项目中):
app.UseMvc();
you should be able to route requests to your api controllers in the fashion required (ie "/api/...") simply by using [Route] attributes as below (example taken from a standard generated ASP.NET 5 Web API application)您应该能够以所需的方式(即“/api/...”)将请求路由到您的 api 控制器,只需使用如下 [Route] 属性(示例取自标准生成的 ASP.NET 5 Web API 应用程序)
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
The above example will route any GET request made to "/api/values".上面的例子将任何 GET 请求路由到“/api/values”。
While this approach can be used to handle requests made to your api, in order to deliver the files needed for your front end javascript application/single page app, you will need to enable static file serving.虽然此方法可用于处理对您的 api 发出的请求,但为了交付前端 javascript 应用程序/单页应用程序所需的文件,您需要启用静态文件服务。 If you add the following to the Configure method in your Startup.cs class:
如果将以下内容添加到 Startup.cs 类中的 Configure 方法:
app.UseStaticFiles();
this will allow your application to serve those static files - by default, these are served from the 'wwwroot' folder, although this can be changed in the project.json file if required.这将允许您的应用程序提供这些静态文件 - 默认情况下,这些文件是从“wwwroot”文件夹提供的,尽管如果需要,可以在 project.json 文件中更改。 The files needed for your front end app should then be added to this folder.
然后应将前端应用程序所需的文件添加到此文件夹中。 A tutorial on serving static files can befound here .
可以在此处找到有关提供静态文件的教程。
Second Point:第二点:
In my experience this will not be an issue when you publish your website - provided your server is set up correctly, you will not need to include the port when making a request - navigating to [yourwebsitename]/api/... will suffice.根据我的经验,当您发布网站时,这不会成为问题 - 只要您的服务器设置正确,您在发出请求时就不需要包含端口 - 导航到 [yourwebsitename]/api/... 就足够了。
Third point:第三点:
In my opinion this entirely depends on how large the project is likely to grow, although preference and opinion will vary from developer to developer.在我看来,这完全取决于项目可能增长的规模,尽管偏好和意见因开发人员而异。 Generally speaking, if the project will remain small in scope then keeping both in a single project is perfectly ok, as unnecessary complexity is reduced.
一般来说,如果项目的范围仍然很小,那么将两者保留在一个项目中是完全可以的,因为减少了不必要的复杂性。 However it is also very useful as you have pointed out, to maintain a separation of concerns between projects.
然而,正如您所指出的,它也非常有用,可以保持项目之间的关注点分离。 So aside from the organisational advantage of your approach, the respective dependencies of the two projects are/will be kept separate also.
因此,除了您的方法的组织优势之外,两个项目的各自依赖项也/将保持分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.