简体   繁体   English

在 VS2015 ASP.NET 5 Web API 应用程序中设置项目 url

[英]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:目前我的项目是这样的:

项目层次结构(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 .我在这里看到了这个答案: 将应用程序设置为单独的 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:我想知道的是:

  • Do I have to set this option somewhere else?我必须在其他地方设置这个选项吗? (ie project.json) (即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.理想情况下,我希望[websiteURL]/api提供我的 API,而该链接明确放置了 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.我可以轻松地将 API 和 Web 放在同一个项目中,但我喜欢客户端和服务器端逻辑的分离。

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.

相关问题 用VS2015创建的ASP.Net核心Web应用程序(.Net Framework)创建了一个看上去很狡猾的项目 - ASP.Net Core Web Application (.Net Framework) created with VS2015 creates a dodgy looking project 是否可以在VSCode OSX上运行VS2015中创建的ASP.NET Web项目? - Is it possible to run a ASP.NET web project created in VS2015 on VSCode OSX? 如果网站创建为 Visual C# ASP.Net Web 应用程序在 VS2015 中,PageMethods 不起作用 - PageMethods not working if the website is created as a Visual C# ASP.Net Web Application in VS2015 从VS2015中的ASP.NET Web项目引用VB.NET类库时,Intellisense损坏 - Intellisense broken when referencing a VB.NET class library from a ASP.NET Web project in VS2015 为什么我不能将我的ASP.Net项目从VS2015发布到Azure - Why can't I publish my ASP.Net project from VS2015 to Azure VS2015:安全更改ASP.Net解决方案和项目名称 - VS2015: Safely changing a ASP.Net solution and project name 无法使用VS2015 RTM在ASP.NET 5项目上还原nuget程序包 - Can't restore nuget packages on ASP.NET 5 project with VS2015 RTM 在VS2015中创建ASP.NET MVC5项目应该选择哪个模板? - Which template should be select for creating an ASP.NET MVC5 project in VS2015? 如何在安装vs2015之后在vs2015上安装Asp.Net? - How can I install Asp.Net on vs2015 after already installing vs2015 ? 在VS2015中调试asp.net项目时调试xunit - Debug xunit while debugging asp.net project in VS2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM