我正在将 ASP.NET Core 2 应用程序迁移到 ASP.NET Core 3。我的控制器需要返回具有已经是 JSON 字符串的属性的对象,因此它看起来像这样:
public class Thing {
public int Id { get; set; }
public string Name { get; set; }
public string Data { get; set; } // JSON Data
}
var thing = new Thing
{
Id = 1,
Name = "Thing",
Data = "{\"key\":\"value\"}"
};
Run Code Online (Sandbox Code Playgroud)
Thing应序列化,以便该Data属性不是字符串,而是 JSON 对象的一部分。像这样:
{
"id": 1,
"name": "Thing",
"data": {
"key": "value"
}
}
Run Code Online (Sandbox Code Playgroud)
在 .NET Core 2 中,Newtonsoft.Json我使用 的 …
我有一个现有的 ASP.NET Web API 2 项目,以前在路由前缀/api/( example.com/api/Users) 下提供服务,但现在需要将其直接移到根 ( example.com/Users) 下。
问题在于,现在某些路由与项目根目录中的物理目录相匹配,并且 IIS 在发出此类冲突请求时会尝试为这些目录提供服务。
项目结构如下所示:
MyApi/
App_Start/
Controllers/
Models/
...
Permissions/
...
Global.asax
Global.asax.cs
...
Web.config
Run Code Online (Sandbox Code Playgroud)
路由使用属性路由进行映射:config.MapHttpAttributeRoutes();
这是有问题的控制器的简化示例:
namespace MyApi.Controllers
{
[RoutePrefix("Permissions")]
public class PermissionController
{
[Route("")]
[HttpGet]
[ResponseType(typeof(IEnumerable<Permission>))]
public IHttpActionResult Get()
{
return ...
Run Code Online (Sandbox Code Playgroud)
当向 发出请求时example.com/Permissions,IIS 首先响应重定向301至example.com/Permissions/(尾部斜杠),然后响应:
IIS 10.0 Detailed Error
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this …Run Code Online (Sandbox Code Playgroud)