Web API错误-'/ api'应用程序中的服务器错误

时间:2019-10-06 04:17:54

标签: asp.net-web-api asp.net-web-api2

在将Web API发布到AngularJs IIS站点的子项IIS之后,我可以到达“ https://localhost/api”并查看所有端点;但是当我尝试通过GET请求到达某个特定的端点时,出现了错误

  

“ /”应用程序中的服务器错误

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

[RoutePrefix("api/branches")]
public class BranchesController : ApiBaseController
{
    [HttpGet]
    [Route("getBranches")]
    public async Task<JsonResult<List<BranchDto>>> GetActiveBranches()
    {
        var branches = new List<BranchDto>();

        var models = await WarehouseUnitOfWork.BranchRepository.GetActiveBranches();
        if (models != null && models.Count > 0)
        {
            branches.AddRange(AutoMapper.Mapper.Map<List<Branch>, List<BranchDto>>(models));
        }

        return Json(branches, new JsonSerializerSettings
        {
            ContractResolver = new WarehouseCustomContractResolver()
        });
    }
}

有什么办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

针对我的情况,解决方案是将Frontend部署到主要的IIS站点中,并在其中创建一个名为v1的后端应用程序。 然后,在我的angularJS中,我定义了Production应用,以向/v1/api而不是/api发出http请求。

enter image description here