错误500 Web API [英] Error 500 Web API

查看:186
本文介绍了错误500 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去的4个小时内一直在努力解决这个问题。

I've been trying to fix this for the last 4 hours.

我有一个新的Web API项目 - 在开发上100%正常工作,

I have a new Web API Project - which works 100% fine on development,

但是在实时服务器上我收到 500内部服务器错误

BUT on the live server I get a 500 Internal Server Error.

当我部署新版本并直接向 http:// URL / Action 发送请求时,我得到了错误。
如果我先去 http:// URL / 然后将POST请求发送到 http: // URL / Action ,它可以工作。

When I deploy a new version and send a request directly to http://URL/Action I get the error. BUT If I go to http://URL/ first then send the POST request to http://URL/Action, it works.

对于 12,13小时

因此,首先我需要打开: http:// URL /操作,然后发送 POST 请求。

So for it to work first I have to open: http://URL/Action, then send the POST request.

因此项目使用 Ninject ,这是 Startup.cs

using System.Collections.Generic;
using System.Reflection;
using System.Web.Http;
using API.MyApi;
using Business.Bindings;
using Microsoft.Owin;
using Ninject;
using Ninject.Modules;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;

[assembly: OwinStartup(typeof(Startup))]
namespace API.MyApi
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            WebApiConfig.Register(config);
            app.UseWebApi(config);

            app.UseNinjectMiddleware(CreateKernel);
            app.UseNinjectWebApi(config);

            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =  IncludeErrorDetailPolicy.Always;
        }

        private static StandardKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            var modules = new List<INinjectModule>
            {
                new BusinessBindings(),
                new DataBindings()
            };
            kernel.Load(modules);
            return kernel;
        }

    }
}

WebAPI配置

using System.Web.Http;

namespace API.MyApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        }
    }
}

我试过添加这一行获取整个错误消息视图的代码:

I have tried adding this line of code to get a view of the entire error message:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

并将其添加到 web.config file

and also adding this to the web.config file

<customErrors mode="Off"/>

但是我在PostMan中得到的错误是:

But the error I get in PostMan is:

在浏览器中导航到URL.com后:

After I navigate to URL.com in the Browser:

然后如果我发送POST请求:

Then If I send the POST request:

编辑:

这是我的异常处理程序属性:

This is my exception handler attribute:

public class ExceptionHandlerAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        var exception = actionExecutedContext.Exception;

        using (var exceptionLogger = new CustomLogger("Exceptions"))
        {
            exceptionLogger.LogExceptionMessage(exception.Message, exception.InnerException, exception.StackTrace);
        }

        base.OnException(actionExecutedContext);
    }

}

并且控制器装饰有属性:

and the Controller is decorated with the attribute:

[ExceptionHandler]
public class SmartOneCController : BaseApiController
{
    public SmartOneCController(CustomLogger logger)
    {
    }

}

那么这个属性应该做的是将异常记录到一个日志文件 - 它正在开发和部署版本上进行测试。

So what this Attribute should do is Log the exception to a log file - Which is working tested on both the Development and the deployed version.

但是没有记录异常从 500内部服务器错误

控制器代码,我看不到控制器代码如何在这里提供帮助,因为在访问API的基本URL之后一切正常: URL.com 之后我可以调用这个POST方法,一切正常。

Controller Code, I don't see how the controller code will help here, as everything works after visiting the Base URL of the API: URL.com after that I can call this POST method and everything works.

[ExceptionHandler]
public class SmartOneCController : BaseApiController
{
    public SmartOneCController(CustomLogger logger, IAssetsProvider assetsProvider, ISatelliteTrackerProvider satelliteTrackerProvider) : base(logger, assetsProvider, satelliteTrackerProvider)
    {
    }

    public void Post(HttpRequestMessage request)
    {
        try
        {

            // Reading data as XML string to log to files - In case message structure is changed
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
            var str = xmlDoc.InnerXml;

            _logger.LogMessage(MessageType.Information, string.Format("RAW XML Message: {0}", str));

            // Convert to model

            var model = XMLHelper.FromXml<trackermessages>(str);
 ......


推荐答案

问题在于Startup.cs类,这就是它的工作原理

The Problem was with the Startup.cs class, this is what got it working

[assembly: OwinStartup(typeof(Startup))]
namespace API.MyApi
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var webApiConfiguration = new HttpConfiguration();

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

            app.UseNinjectMiddleware(CreateKernel);
            app.UseNinjectWebApi(webApiConfiguration);
        }

        private static StandardKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            var modules = new List<INinjectModule>
            {
                new BusinessBindings(),
                new DataBindings()
            };
            kernel.Load(modules);
            return kernel;
        }

    }
}

这篇关于错误500 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆