【问题标题】:ASP.NET MVC How to debug a published folderASP.NET MVC 如何调试已发布的文件夹
【发布时间】:2020-11-09 12:19:28
【问题描述】:

早安。

我是 asp.net mvc 的新手。

我使用 asp.net mvc 创建了一个简单的 Web 应用程序。

该应用程序用于将图像上传到文件夹。该应用程序在visual studio中运行良好,但是一旦我发布并戴上iis,它就不会上传图像。

我在想是否有办法调试已发布的版本,以便我可以得到发布的位置?

请帮助我如何调试已发布的版本以及如何解决问题。

下面是它发现错误的地方

 string path = System.IO.Path.Combine(targetLocation, file.FileName);

【问题讨论】:

  • 研究“远程调试”。

标签: c# asp.net .net asp.net-mvc visual-studio


【解决方案1】:

我建议按照以下几点对 ASP.NET MVC 应用程序进行调试。

服务器端 - 您应该使用异常过滤器将任何问题写入文本文件并进行跟踪

前端 - 浏览器级别的开发者工具,用于 javascript 相关的东西。

DataBase Side - 您可以使用 SQL profiler 来捕获服务器端调试。

【讨论】:

    【解决方案2】:

    在您的代码中添加 try catch 块

    并在 catch 代码中将异常错误消息记录到服务器中的文件中,以查看确切的错误如下:

    将此模型添加到“模型”文件夹下或任何地方的解决方案中

    public class ExceptionLogger
        {
            private string sLogFormat;
    
        
            public void ErrorLog(string sErrMsg)
            {
                try
                {
                    string LogDirectory = "C:\LogFiles\";
                    CheckCreateLogDirectory(LogDirectory);
                    LogDirectory = (LogDirectory + "Log_" + DateTime.Now.ToString("dd_MM_yyyy", new CultureInfo("en-us")) + ".txt");
                    sLogFormat = DateTime.Now.ToString("dd/MM/yyyy", new CultureInfo("en-us")) + " " + DateTime.Now.ToString("HH:mm:ss", new CultureInfo("en-us")) + " ==> ";
                    StreamWriter sw = new StreamWriter(LogDirectory, true);
                    sw.WriteLine(sLogFormat + sErrMsg);
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception e) { 
                }
            }
            private bool CheckCreateLogDirectory(string LogPath)
            {
                bool loggingDirectoryExists = false;
                DirectoryInfo oDirectoryInfo = new DirectoryInfo(LogPath);
                if (oDirectoryInfo.Exists)
                {
                    loggingDirectoryExists = true;
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(LogPath);
                        loggingDirectoryExists = true;
                    }
                    catch
                    {
                        throw new Exception();
                        // Logging failure
                    }
                }
                return loggingDirectoryExists;
            }
            
        }
    

    然后在可能发生错误的控制器或存储库中执行以下操作:

    a- 在顶部添加此代码:

    ExceptionLogger Err = new ExceptionLogger(); 
    //you need to write "using" in top to refactor the import error
    // something like this: using YourProjectName.Models; 
    

    b- 在你的代码块中添加 try catch 块

    try{
        //your code 
    }catch(Exception ex){
        Err.ErrorLog(ex.Message + " --- , ---- " + ex.InnerException);
    }
    

    另一种方法是在您的 BaseController 上添加 OnException 事件,该事件将检测任何错误或异常,因此您可以在没有 try-catch 块的情况下记录它,如下所示:

    在你的 baseController 中定义它:ExceptionLogger Err = new ExceptionLogger(); 并使用它,然后添加事件:

    protected override void OnException(ExceptionContext filterContext)
    {
        var isSent = false;
               
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
        {
            return;
        }
        if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
        {
            return;
        }
                
        if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
        {
            filterContext.Result = new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    error = true,
                    message = filterContext.Exception.Message
                }
            };
            LogError("Controller: AjaxCall" + " Action: AjaxCall" + filterContext.Exception.Message, filterContext.Exception);
    
        }
        else if (filterContext.Exception is HttpAntiForgeryException)
        {
            Response.Clear();
            Server.ClearError(); //make sure you log the exception first 
            filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary(new { action = "Logout", controller = "Account" }));
    
        }
        else
        {
            var controllerName = (string)filterContext.RouteData.Values["controller"];
            var actionName = (string)filterContext.RouteData.Values["action"];
            var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                     
    
            if (actionName == "_Menu")
            {
                isSent = true;
                Response.Redirect("~/Account/Logout");
            }
            filterContext.Result = new ViewResult
            {
                ViewName = "~/Views/Error/Error.cshtml",
                MasterName = null,
                ViewData = new ViewDataDictionary(model),
                TempData = filterContext.Controller.TempData
            };
            ////Redirect or return a view, but not both.
            LogError("Controller: " + controllerName + " Action: " + actionName + filterContext.Exception.Message, filterContext.Exception);
        }
    
        // log the error by using your own method
    
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        if (isSent == false)
        {
            filterContext.HttpContext.Response.StatusCode = 500;
        }
    
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
    
    
    private void LogError(string message, Exception exception)
    {
        Err.ErrorLog(message + " && " + exception.Message);
    }
    

    现在发生错误时,您将转到服务器中的 c:/logfiles 文件夹并找到发生的确切错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      相关资源
      最近更新 更多