使用.Net Core

时间:2018-10-15 15:15:15

标签: c# rest visual-studio-2017 asp.net-core-2.1

实际上,这是第一次尝试创建REST Web服务并处理任何Web东西。一直遵循本指南:http://www.patrickschadler.com/creating-a-rest-webservice-with-net-core/

它使用了非常简单的方法(类似于这种方法,因为我不需要选择“ Web应用程序”选项时添加的所有膨胀软件)。

但是,按照我的示例:

  1. 按原样运行程序(看到世界问候)
  2. 然后添加了读写控制器
  3. 已将项目添加到Startup.cs

然后尝试调用:https://localhost:44325/api/r_w,这产生了404响应,我可以在项目调试输出窗口以及浏览器中看到该响应。

我环顾四周,很多人使用 Web应用程序选择添加了一些其他文件和依赖项,而解决这些问题的方法无济于事。

最初,我认为这与路线的书写方式有关:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");

        });

但是,不幸的是,由于我搜索的一些解决方案建议更改路线对我没有任何影响。

我什至尝试使用routedebugger,但是在解决方案资源管理器中的 Dependencies / Nuget 下,它显示存在兼容性问题,将无法正常工作。

然后发现这非常有帮助: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1

下面是启动文件:

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //app.Run(async (context) =>
        //{
        //    await context.Response.WriteAsync("Hello World!");
        //});
        app.UseStaticFiles();
        //app.UseMvc();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");

        });
    }
}

来自读/写控制器的一小段代码(与人们将其添加到项目中时完全相同):

namespace deliver_data.wwwroot.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class r_wController : ControllerBase
{
    // GET: api/r_w
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

真的很想让这个例子生效,这似乎是最简单的例子。只是不是真的了解为什么它不起作用。从我的角度来看,是否存在对某种事物的严重缺乏了解?

2 个答案:

答案 0 :(得分:1)

您的命名空间看起来很不正常-这表明您的文件夹结构看起来像这样:

  • wwwroot
    • 控制器
      • r_wController.cs
  • deliver_data.csproj
  • ...

位于wwwroot内部的控制器类将不会被拾取:这是ASP.NET Core中的一个特殊文件夹,代表您希望应用程序服务的原始资源。

为解决此问题,将Controllers文件夹拉到wwwroot文件夹之外的某个级别,如下所示:

  • 控制器
    • r_wController.cs
  • wwwroot
    • ...
  • deliver_data.csproj
  • ...

答案 1 :(得分:0)

尝试:

namespace deliver_data.wwwroot.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class r_wController : ControllerBase
    {
       // GET: api/r_w
       [HttpGet]
       public IHttpActionResult Get()
       {
           return new string[] { "value1", "value2" };
       }