使用 .Net Core 在 Web 服务上收到 404 错误
Received 404 Error on Webservice using .Net Core
所以真的是第一次尝试创建 REST 网络服务并处理任何网络内容。一直遵循本指南:http://www.patrickschadler.com/creating-a-rest-webservice-with-net-core/
它使用了一种非常简单的方法(就像这种方法一样,因为我不需要在选择“Web 应用程序”选项时添加的所有英国媒体报道软件)。
但是,正如我所遵循的示例:
- 运行 程序原样(看到 hello world 响应)
- 然后添加读写控制器
- 已将项目添加到 Startup.cs
然后尝试调用:https://localhost:44325/api/r_w,这导致了 404 响应,我可以在项目调试输出 window 和浏览器中看到该响应。
我环顾四周发现很多人使用 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?}");
});
}
}
来自 read/write 控制器的一小段代码(与人们将其添加到项目时完全相同):
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" };
}
真的很想让这个例子工作,这似乎是最简单的一个。只是不太明白为什么它不起作用。从我的角度来看,是否存在对某事的严重缺乏理解?
试试:
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
- deliver_data.csproj
- ...
位于 wwwroot
内部的控制器 类 将不会被拾取:这是 ASP.NET Core 中的一个特殊文件夹,代表您希望应用程序提供服务的原始资源。
为了解决这个问题,请将您的 Controllers
文件夹向上拉到 wwwroot
文件夹之外,如下所示:
- 控制器
- r_wController.cs
- wwwroot
- ...
- deliver_data.csproj
- ...
所以真的是第一次尝试创建 REST 网络服务并处理任何网络内容。一直遵循本指南:http://www.patrickschadler.com/creating-a-rest-webservice-with-net-core/
它使用了一种非常简单的方法(就像这种方法一样,因为我不需要在选择“Web 应用程序”选项时添加的所有英国媒体报道软件)。
但是,正如我所遵循的示例:
- 运行 程序原样(看到 hello world 响应)
- 然后添加读写控制器
- 已将项目添加到 Startup.cs
然后尝试调用:https://localhost:44325/api/r_w,这导致了 404 响应,我可以在项目调试输出 window 和浏览器中看到该响应。
我环顾四周发现很多人使用 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?}");
});
}
}
来自 read/write 控制器的一小段代码(与人们将其添加到项目时完全相同):
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" };
}
真的很想让这个例子工作,这似乎是最简单的一个。只是不太明白为什么它不起作用。从我的角度来看,是否存在对某事的严重缺乏理解?
试试:
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
- 控制器
- deliver_data.csproj
- ...
位于 wwwroot
内部的控制器 类 将不会被拾取:这是 ASP.NET Core 中的一个特殊文件夹,代表您希望应用程序提供服务的原始资源。
为了解决这个问题,请将您的 Controllers
文件夹向上拉到 wwwroot
文件夹之外,如下所示:
- 控制器
- r_wController.cs
- wwwroot
- ...
- deliver_data.csproj
- ...