【发布时间】:2019-11-06 19:59:20
【问题描述】:
客户 ID:1147947322680754 帕特:2a22564a5ce15dd46ee66468097ad0b9
在阅读和挖掘源代码数小时后,尝试在我的新 api 上配置属性路由无法完成。你是我最后的手段。我做错了什么?
我配置了 app.UseMvc();在启动并使用以下代码...
这是 BaseController 的外观;
[ApiController]
public abstract class GKControllerBase : ControllerBase
{
private readonly IHttpContextAccessor _accessor;
public GKControllerBase(IHttpContextAccessor accessor)
{
this._accessor = accessor;
}
}
这是 UserController.cs 的外观;
[ApiController]
[Route("Services/[controller]/[action]")]
public class UserController : GKControllerBase
{
public UserController(IHttpContextAccessor accessor) : base(accessor)
{
}
[HttpGet] //Services/User/Logout
[UserTypeFilter(UserType.Anonymous, UserTypeFilterMode.SingularDeny, ErrorMessage = "You are not logged in!")]
public object Logout()
{
if (GKManagers.SessionManager.TryGetCurrentUser(base.HttpContext, out User user))
{
GKManagers.SessionManager.Logout(base.HttpContext, user.UniqueId);
return this.Answer(HttpStatusCode.OK, null, "Logout successful");
}
return this.Answer(HttpStatusCode.InternalServerError);
}
[HttpGet("{username}/{password}")] //Services/User/Login/username/password
[UserTypeFilter(UserType.Anonymous, UserTypeFilterMode.SingularAllow, ErrorMessage = "You are already logged in!")]
public object Login(string username, string password)
{
if (GKManagers.SessionManager.TryLogin(base.HttpContext, username, password, out User user))
return this.Answer(HttpStatusCode.OK, user, "Login successful");
return this.Answer(HttpStatusCode.InternalServerError);
}
}
这是 GroupController.cs 的外观;
[ApiController]
[Route("Services/[controller]/[action]")]
public class GroupController : GKControllerBase
{
public GroupController(IHttpContextAccessor accessor) : base(accessor)
{
}
[HttpGet("{groupID:int}")] //Services/Group/Join/1
[UserTypeFilter(UserType.Anonymous, UserTypeFilterMode.SingularDeny, ErrorMessage = "You are not logged in!")]
[PermissionFilter
(Data.Enums.ApplicationContext.Membership_Group, Operation.CanJoin, ErrorMessage = "You are not permitted to join the %groupName%. You might be already in it.")]
public object Join(Int32 groupID)
{
if (GKManagers.SessionManager.TryGetCurrentUser(this.HttpContext, out User currentUser))
{
GKManagers.Database.Membership.GroupMembership.Add(new GroupMembership()
{
Group = groupID,
User = currentUser.UserId,
IsActive = true,
JoinDate = DateTime.Now,
LastActionDate = DateTime.Now,
Status = (Int32)Status.Active,
});
}
return this.Answer(HttpStatusCode.InternalServerError);
}
}
我正在尝试将路由配置为看起来像属性旁边的注释部分。
提前致谢!
【问题讨论】:
-
我在asp.net core 3.0 web api项目中使用你的代码(去掉过滤器和无关代码)没有问题。你在新项目中试过了吗?那些过滤器属性对路由?
标签: asp.net-core asp.net-mvc-routing asp.net-core-webapi .net-core-3.0 asp.net-core-3.0