MVC应用程序:Url Helper抛出HttpException“类型转换失败!”

时间:2015-11-16 11:41:31

标签: c# asp.net-mvc-5

我在团队项目(MVC应用程序)中工作。在我进行更新后,应用程序开始提供HttpException“类型转换失败!”我不知道为什么。

代码:

public static class UrlHelper
{
    private static System.Web.Mvc.UrlHelper _urlHelper;

    public static System.Web.Mvc.UrlHelper GetFromContext()
    {
        if (_urlHelper == null)
        {
            if (HttpContext.Current == null)
            {
                throw new HttpException("Current httpcontext is null!");
            }

            if (!(HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))
            {
                throw new HttpException("Type casting is failed!");
            }

            _urlHelper = new System.Web.Mvc.UrlHelper(((System.Web.Mvc.MvcHandler)HttpContext.Current.CurrentHandler).RequestContext);
        }

        return _urlHelper;
    }
}

以下代码行中的错误ocorre和'System.Web.Mvc.dll'和'System.Web.Mvc.dll'中存在“类型'System.Web.Mvc.MvcHandler'”

if (!(HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))

任何想法?

[编辑更多信息]

  • 我尝试以下事情但没有成功。
  • 我清理解决方案。
  • 我删除了项目的bin和所有依赖项的bin 它们。
  • 我检查了我生气的变化(在主项目中和 在其他人试图查看他们是否有错误的影响。

实用信息:

  • 将Visual Studio Ultimate 2013与Update 3一起使用 12.0.30723.00
  • 使用Microsoft .NET FrameWork,版本4.5.51650

  • 使用System.Web,版本4.0.30319

  • 使用MVC 5,版本5.1.0.0

2 个答案:

答案 0 :(得分:0)

试试这个:

 if (!(System.Web.HttpContext.Current.CurrentHandler is System.Web.Mvc.MvcHandler))
 {
       throw new HttpException("Type casting is failed!");
 }

答案 1 :(得分:0)

问题出在web.config文件中。在他们看来,有些配置不对。正确完成所有配置后,问题就解决了。我不确切知道导致这个特定错误的原因,我只知道问题是web.config文件