客户ID:1147947322680754 PAT: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);
}
}
我正在尝试将路由配置为看起来像属性旁边的注释部分。
谢谢!
答案 0 :(得分:0)
将控制器路由更改为:
[Route("Services/[controller]")]
然后在操作上尝试以下操作:
[HttpGet("[action]")] // Services/User/Logout
[HttpGet("[action]/{groupID:int}")] // Services/Group/Join/1
[HttpGet("[action]/{username}/{password}")] // Services/User/Login/username/password
或如下所示:
[Route("[action]")] // Services/User/Logout
[HttpGet]
[Route("[action]/{groupID:int}")] // Services/Group/Join/1
[HttpGet]
[Route("[action]/{username}/{password}")] // Services/User/Login/username/password
[HttpGet]