Asp.net MVC3 UrlHelper.Action() 抛出 NullReferenceException

发布于 2024-12-01 09:24:14 字数 1446 浏览 1 评论 0 原文

问题

我目前正在为 ASP.net MVC 3 构建一个帮助程序,并且在考虑 UrlHelper.Action() 方法时遇到了问题。除了第一个请求(应用程序启动后)之外的每个请求,以下代码都会引发 NullReferenceException。

var src = htmlHelper.Url().Action("Css", "Asset", options);

相关堆栈

System.Web.HttpServerVarsCollection.Get(String name) +8740566
   System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext) +42
   System.Web.Mvc.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext) +23
   System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) +163
   System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath) +63
   System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) +150
   System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues) +55

可能的原因

我使用一个名为 AttributeRouting 的库,我通过 nuget 安装该库,并认为这可能会导致问题,但删除引用没有效果。

因为它确实可以在第一个请求中工作,但此后的每个请求都会失败,我感觉它与在应用程序启动时运行但应在请求启动时运行的代码有关,或者某些变量/对象不会在请求中保留。

额外信息

htmlHelper.Url() 是以下扩展方法。

public static UrlHelper Url(this HtmlHelper helper)
{
    return new UrlHelper(helper.ViewContext.RequestContext);
}

Problem

I am currently building a helper for ASP.net MVC 3 and am running into a problem considering the UrlHelper.Action() method. Every request except for the first one (after the application start) the following code throws a NullReferenceException.

var src = htmlHelper.Url().Action("Css", "Asset", options);

Relevant Stack

System.Web.HttpServerVarsCollection.Get(String name) +8740566
   System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext) +42
   System.Web.Mvc.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext) +23
   System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath) +163
   System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath) +63
   System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) +150
   System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues) +55

Possible Cause

I use a lib called AttributeRouting which I installed through nuget and thought this might cause the problem but removing the reference has no effect.

Because it does work the first request but fails for every request after that I have a feeling it has something to do with code that runs at the application start but should run at request start or that certain variables/objects aren't persisted over the requests.

Extra Info

htmlHelper.Url() is the following extention method.

public static UrlHelper Url(this HtmlHelper helper)
{
    return new UrlHelper(helper.ViewContext.RequestContext);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-12-08 09:24:14

var src = htmlHelper.Url().Action("Css", "Asset", options); 您在视图或控制器之一中使用它吗?

无论哪种方式,两者都应该定义自己的 Url 对象(我在这里假设您使用的 htmlHelper 对象是您自己创建的)。

public class MyController : Controller
{
    ....
    public ActionResult Index()
    {
        var src = Url.Action("Css", "Asset", options);
        ....
    }
}

或者在您看来:

<head>
    <meta charset="UTF-8">
    <title>@ViewBag.Title</title>
    @{
        var src = Url.Action("Css", "Asset", options);
    }
    <link rel="Stylesheet" href="@src" />

或者更好的是,只需

var src = htmlHelper.Url().Action("Css", "Asset", options); Do you use that in a view or in one of your controllers?

Either way, both should have their own Url object defined (I made the assumption here that the htmlHelper object you use is one you created yourself).

public class MyController : Controller
{
    ....
    public ActionResult Index()
    {
        var src = Url.Action("Css", "Asset", options);
        ....
    }
}

or in your view:

<head>
    <meta charset="UTF-8">
    <title>@ViewBag.Title</title>
    @{
        var src = Url.Action("Css", "Asset", options);
    }
    <link rel="Stylesheet" href="@src" />

or better yet, just <link rel="Stylesheet" href="@Url.Action("Css","Asset",options)" />

故乡的云 2024-12-08 09:24:14

你解决过这个问题吗?我最近在安装 IIS 7 Url Rewrite Module v.2.0 时看到了此错误。卸载后问题就消失了。

Did you ever resolve this? I recently saw this error when I installed the IIS 7 Url Rewrite Module v.2.0. After uninstalling it the issue went away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文