邮递员的api请求

时间:2019-02-21 11:52:02

标签: c# api

我有一个mvc应用程序。我正在使用API​​进行检索。老师。

这是课程:

[Authorize(Roles = IdentityRoles.Teacher)]
    [RoutePrefix("api//current")]
    public sealed class CurrentTeacherController : ApiControllerBase
    {
        private readonly ICurrentTeacherProcess _currentTeacherProcess;

        /// <summary>
        /// Constructor.
        /// </summary>
        public CurrentTeacherController(ICurrentTeacherProcess process)
        {
            _currentTeacherProcess = process;
        }

        /// <summary>
        /// Gets the teacher data of the current user
        /// </summary>
        /// <returns>The TeacherDto of the current teacher</returns>
        [Route("")]
        [HttpGet]
        public TeacherDto GetTeacher()
        {
            return _currentTeacherProcess.GetTeacher();
        }

}

我正在用邮差(Postman)找老师。例如ID为1001的老师。

然后在邮递员中输入:

http://localhost:6598/api/register/

But I get a : 404 not found exception.

这是startup.cs文件:

 public sealed class Startup
    {
        /// <summary>
        /// Configures the application for use with OWIN. This method is called implicitly by Microsoft.Owin.Host.SystemWeb.
        /// </summary>
        /// <param name="app"><see cref="IAppBuilder" />implementation.</param>
        public void Configuration(IAppBuilder app)
        {
            var config = GlobalConfiguration.Configuration;

            WebApiConfig.Register(config);

            var container = AutofacConfig.Register(config);

            app.UseAutofacLifetimeScopeInjector(container);

            app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
            {
                AuthenticationType = "Hallo",
                AuthenticationMode = AuthenticationMode.Active,
                TokenValidationParameters = ApiGatewaySecurityTokenHandler.CreateTokenValidationParameters(
                    "hoi", "hoi", IdentityRoles.Duo, IdentityRoles.Competent, IdentityRoles.Berichtenbox),
                TokenHandler = container.Resolve<JwtSecurityTokenHandler>()
            });

            app.UseStageMarker(PipelineStage.PostAuthenticate);

            app.UseMiddlewareFromContainer<ApiGatewayMiddleware>();

            app.UseAutofacWebApi(config);
            app.UseWebApi(config);
        }
    }

使用startup.cs文件更新

1 个答案:

答案 0 :(得分:1)

您收到404,因为该请求的API中不存在处理程序。您需要像这样将ID添加到您的API路由

[Route("{id}")]
[HttpGet]
public TeacherDto GetTeacher(int  id/* or other type if you aren't using int as your primary key*/ )
{
    return _currentTeacherProcess.GetTeacher(id);
}

您的请求应为:

http://localhost:6598/api/register/teachers/current/1001