简体   繁体   中英

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. Currently my project looks like this:

项目层次结构(2 个项目,API 和 Web)

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 .

But for ASP.NET 5 projects, the properties only have 3 tabs (as opposed to the several found in ASP.NET 4 projects):

属性(应用程序、构建、调试)

What I'm wondering is:

  • Do I have to set this option somewhere else? (ie project.json)
  • How would this work when I publish? Ideally I'd want [websiteURL]/api to serve up my API, whereas that link explicitly put localhost:8080.
  • Is having these as two projects a good idea? I could easily put API and web in the same project, but I like the separation of client-side and server-side logic.

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. 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):

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)

    [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".

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. If you add the following to the Configure method in your Startup.cs class:

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. 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.

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.

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