Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.Net Core在Webservice上收到404错误_C#_Rest_Asp.net Core_Visual Studio 2017_Asp.net Core 2.1 - Fatal编程技术网

C# 使用.Net Core在Webservice上收到404错误

C# 使用.Net Core在Webservice上收到404错误,c#,rest,asp.net-core,visual-studio-2017,asp.net-core-2.1,C#,Rest,Asp.net Core,Visual Studio 2017,Asp.net Core 2.1,因此,这是第一次尝试创建REST web服务并处理任何web内容。我们一直遵循本指南: 它使用了一种非常简单的方法(类似于这种方法,因为我不需要在选择“Web应用程序”选项时添加的所有膨胀软件) 然而,正如我所举的例子: 按原样运行程序(查看hello world响应) 然后添加读写控制器 已将项目添加到Startup.cs 然后尝试调用:,这导致了404响应,我可以在项目调试输出窗口和浏览器中看到该响应 我环顾四周,许多人使用Web应用程序选择添加了一些其他文件和依赖项,这些问题的解决方案没有

因此,这是第一次尝试创建REST web服务并处理任何web内容。我们一直遵循本指南:

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

然而,正如我所举的例子:

  • 按原样运行程序(查看hello world响应)
  • 然后添加读写控制器
  • 已将项目添加到Startup.cs
  • 然后尝试调用:,这导致了404响应,我可以在项目调试输出窗口和浏览器中看到该响应

    我环顾四周,许多人使用Web应用程序选择添加了一些其他文件和依赖项,这些问题的解决方案没有帮助

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

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
    
            });
    
    然而,不幸的是,我所寻找的一些解决方案表明,改变路线对我没有任何影响

    我甚至尝试使用RoutedBugger,但是在解决方案资源管理器中的Dependencies/Nuget下,它显示兼容性存在问题,无法按预期工作

    然后发现这非常有用:

    以下是启动文件:

    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
    {
    [路由(“api/[控制器]”)]
    [ApiController]
    公共类r\U控制器:ControllerBase
    {
    //获取:api/r\w
    [HttpGet]
    公共IEnumerable Get()
    {
    返回新字符串[]{“value1”,“value2”};
    }
    
    我真的很想让这个例子起作用,这似乎是最简单的一个。只是不太明白为什么它不起作用。从我的观点来看,是不是对某些东西缺乏理解?

    试试:

    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" };
           }
    
    此处的名称空间看起来很不寻常-它表明您有一个类似以下内容的文件夹结构:

    • wwwroot
      • 控制器
        • r_wController.cs
    • 交付_data.csproj
    将不会拾取位于
    wwwroot
    中的控制器类:这是ASP.NET Core中的一个特殊文件夹,表示希望应用程序服务的原始资源

    要解决此问题,请将
    Controllers
    文件夹拉到
    wwwroot
    文件夹之外,如下所示:

    • 控制器
      • r_wController.cs
    • wwwroot
    • 交付_data.csproj

    为什么会有不同?它甚至会编译吗?您是否尝试过没有任何操作的路由:模板:“{controller=Home}/{id?}”?仅供参考,您正在使用
    MapRoute
    应用的模板不适用于
    r\u wController
    ,原因很简单(使用
    route
    HttpGet
    )从本质上说,它优先于。
    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" };
           }