MVC 成员资格 (OpenID) 控制器的 ASP MVC 空引用异常

发布于 2024-07-17 14:45:48 字数 7162 浏览 2 评论 0 原文

我要提前道歉。 我是 ASP.NET 和 MVC 的新手,还不了解内部结构和操作流程。 我正在尝试将 ASP.NET MVC Membershp Starter 套件集成到我正在开发的网站中OpenID 支持。

我已经解决了几个问题,但我很困惑从哪里开始寻找我不断遇到的空引用异常。 流程如下:

  1. 用户在页面上单击“登录”,
  2. MVC MembershipAdministrationController 已正确触发。 它检测到当前没有登录的用户,因此使用登录表单 (Login.aspx) 进行响应。
  3. 渲染此表单时抛出空引用异常。 具体来说,抛出异常的部分是:

    <% using(Html.Form( "OpenID", "Login" )){ %>; 
        
    <图例>登录
    <% =Html.TextBox("openid_identifier" )%>
    ;
    <% =Html.SubmitButton( "提交", "登录", new { onclick = "return starterKit_mvc_membership_validateOpenIdLogin();" } )%>
    ; <% } %>

Html.Form("OpenID","Login")的调用就是抛出异常的调用。 这是调用堆栈:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   at System.Web.Routing.ParsedRoute.Bind(RouteValueDictionary currentValues, RouteValueDictionary values, RouteValueDictionary defaultValues, RouteValueDictionary constraints)
   at System.Web.Routing.Route.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at Microsoft.Web.Mvc.FormExtensions.Form(HtmlHelper helper, String controllerName, String actionName, FormMethod method, IDictionary`2 htmlAttributes)
   at Microsoft.Web.Mvc.FormExtensions.Form(HtmlHelper helper, String controllerName, String actionName)
   at ASP.views_openid_login_aspx.__RenderopenIdLoginContent(HtmlTextWriter __w, Control parameterContainer) in d:\Data\Personal\purplemartin.tv\mvc\MvcMembership\Views\OpenID\Login.aspx:line 15
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Data\Personal\purplemartin.tv\mvc\MvcMembership\Views\Shared\Site.Master:line 46
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Page.Render(HtmlTextWriter writer)
   at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.views_openid_login_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\cada2385\9a6742fc\App_Web_zl9plrr2.1.cs:line 0
   at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
   at System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page)
   at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
   at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

根据要求,控制器如下所示:

    [HandleError( ExceptionType = typeof(OpenIdException) )]
    [HandleError( ExceptionType = typeof(System.Net.WebException) )]
    public virtual ActionResult Login( string openid_identifier )
    {
        //### set page title & declare variables
        ViewData["Title"] = "Login via OpenID";
        var errors = new List<string>();
        var rememberMe = false;
        var openid = new OpenIdRelyingParty();

        //### stage 1: display login form to user
        if( openid.Response == null && Request.HttpMethod != "POST" )
            return View();

显然,这不是完整的控制器,但这是第一次执行的全部内容(呈现登录表单时)。 第一次openid.Response为null,请求方法为"POST"

我从哪里开始寻找问题? 看起来这可能与路由有关,但我不确定。

I'm going to apologize ahead of time. I'm new to ASP.NET and MVC and don't yet grok the internals and flow of operations. I'm trying to get the ASP.NET MVC Membershp Starter Kit integrated into a site I'm developing to get OpenID support.

I've worked through several issues, but I'm stumped at where to start looking for a null reference exception that I keep getting. Here's the flow:

  1. User clicks "Login" on the page
  2. MVC MembershipAdministrationController is properly fired. It detects that there no currently logged-in user and so responds with a login form (Login.aspx).
  3. A null reference exception is thrown while rendering this form. Specifically, the section that throws the exception is:

    <% using(Html.Form( "OpenID", "Login" )){ %>
      <fieldset class="MvcMembership">
        <legend>Login</legend>
        <div><label for="openid_identifier">OpenID Url:</label> <% =Html.TextBox( "openid_identifier" )%></div>
        <div><% =Html.SubmitButton( "submit", "Login", new { onclick = "return starterKit_mvc_membership_validateOpenIdLogin();" } )%></div>
      </fieldset>
    <% } %>
    

The call to Html.Form ("OpenID", "Login") is the call throwing the exception. Here's the call stack:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   at System.Web.Routing.ParsedRoute.Bind(RouteValueDictionary currentValues, RouteValueDictionary values, RouteValueDictionary defaultValues, RouteValueDictionary constraints)
   at System.Web.Routing.Route.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at Microsoft.Web.Mvc.FormExtensions.Form(HtmlHelper helper, String controllerName, String actionName, FormMethod method, IDictionary`2 htmlAttributes)
   at Microsoft.Web.Mvc.FormExtensions.Form(HtmlHelper helper, String controllerName, String actionName)
   at ASP.views_openid_login_aspx.__RenderopenIdLoginContent(HtmlTextWriter __w, Control parameterContainer) in d:\Data\Personal\purplemartin.tv\mvc\MvcMembership\Views\OpenID\Login.aspx:line 15
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Data\Personal\purplemartin.tv\mvc\MvcMembership\Views\Shared\Site.Master:line 46
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Control.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.Page.Render(HtmlTextWriter writer)
   at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.views_openid_login_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\cada2385\9a6742fc\App_Web_zl9plrr2.1.cs:line 0
   at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
   at System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page)
   at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
   at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

As requested, here is what the controller looks like:

    [HandleError( ExceptionType = typeof(OpenIdException) )]
    [HandleError( ExceptionType = typeof(System.Net.WebException) )]
    public virtual ActionResult Login( string openid_identifier )
    {
        //### set page title & declare variables
        ViewData["Title"] = "Login via OpenID";
        var errors = new List<string>();
        var rememberMe = false;
        var openid = new OpenIdRelyingParty();

        //### stage 1: display login form to user
        if( openid.Response == null && Request.HttpMethod != "POST" )
            return View();

Obviously, this isn't the complete the controller, but this is all that executes the first time (when the login form is rendered). The first time openid.Response is null and the request method is "POST".

Where do I begin looking for the problem? It looks like it might have something to do with routing, but I'm not sure.

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

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

发布评论

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

评论(4

痞味浪人 2024-07-24 14:45:48

Simon,您实际上是指“System.Web.Mvc.Html.FormExtensions”,而不是“Microsoft.Web.Mvc.FormExtensions”吗? 有“Microsoft.Web.Mvc.FormExtensions”类(http://aspnet .codeplex.com/SourceControl/changeset/view/22929#266407),但没有 Form 方法。 也许您正在使用旧版本的 Microsoft.Web.Mvc.dll ? 在 Reflector 中打开 Microsoft.Web.Mvc.dll - 在我的机器上 Microsoft.Web.Mvc.dll 的版本为 1.0.0.0,有 4 种方法:

public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, IDictionary<string, object> htmlAttributes) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, object htmlAttributes) where TController: Controller;

声明类型:Microsoft.Web.Mvc.FormExtensions

程序集:Microsoft.Web.Mvc ,Version=1.0.0.0

更新:

我已下载ASP.Net MVC 会员入门工具包预览 5 (0.5)
bin 文件夹中的 Microsoft.Web.Mvc.dll 有 1.0.0.0 版本,但它比 ASP.NET MVC v1.0 的 Microsoft.Web.Mvc.dll 旧。 您可以下载最新版本的 Microsoft.Web.Mvc.dll 那里

Simon, did you actually mean 'System.Web.Mvc.Html.FormExtensions', not 'Microsoft.Web.Mvc.FormExtensions'? There is 'Microsoft.Web.Mvc.FormExtensions' class (http://aspnet.codeplex.com/SourceControl/changeset/view/22929#266407) but there is no Form method. Maybe you are using old version of Microsoft.Web.Mvc.dll ? Open your Microsoft.Web.Mvc.dll in Reflector - on my machine Microsoft.Web.Mvc.dll have version 1.0.0.0 and there are 4 methods:

public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, IDictionary<string, object> htmlAttributes) where TController: Controller;
public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, object htmlAttributes) where TController: Controller;

Declaring Type: Microsoft.Web.Mvc.FormExtensions

Assembly: Microsoft.Web.Mvc, Version=1.0.0.0

UPDATED:

I've dowloaded ASP.Net MVC Membership Starter Kit Preview 5 (0.5).
Microsoft.Web.Mvc.dll from bin folder have 1.0.0.0 vesion, but it is older than Microsoft.Web.Mvc.dll for ASP.NET MVC v1.0. You can download last version of Microsoft.Web.Mvc.dll there.

五里雾 2024-07-24 14:45:48

由于我对这个问题的回应很少,因此我开始寻找将 OpenID 集成到 ASP.NET MVC 中的替代方案。 我发现 RPXLib 这是一个很好的 .NET 和 MVC 包装器 RPXNow。 这是来自多个供应商的 OpenID 登录 Web 服务方法。

这不是一个完美的解决方案,因为我必须编写 openid 凭据和本地配置文件之间的映射代码,但它足以满足我的需求。

我仍然有兴趣通过 Html.Form("controller", "action") 抛出空引用异常来解决原始问题。 我尝试使用通用版本获得相同的结果:Html.Form(c => c.Action())

Since I've gotten very little response for this question, I've starting looking at alternatives for integrating OpenID into ASP.NET MVC. I've found RPXLib which is a nice .NET and MVC wrapper for RPXNow. This is a web-service approach to OpenID login from multiple vendors.

This is not a perfect solution since I have to write the mapping code between openid credentials and local profiles, but it's sufficient for my needs.

I'm still interested in resolving the original problem with Html.Form("controller", "action") throwing a null reference exception. I've tried using the generic version with the same results: Html.Form<Controller>(c => c.Action()).

一桥轻雨一伞开 2024-07-24 14:45:48

您是否使用 ASP.NET MVC v1.0(不是预览版、测试版或 RC)? 好像v1.0中没有Html.Form,只有Hrml.BeginForm。 如果您查看 ASP.NET MVC v1.0 源代码,就会发现 System.Web.Mvc.Html.FormExtensions 类中没有 Form 方法,只有 BeginForm

Are you using ASP.NET MVC v1.0 (not Preview, Beta or RC)? It seems that there is no Html.Form in v1.0 but only Hrml.BeginForm. If you look at ASP.NET MVC v1.0 source there is no Form method but only BeginForm in System.Web.Mvc.Html.FormExtensions class

镜花水月 2024-07-24 14:45:48

If all else fails, you can still download the ASP.NET MVC source, hook it up to your project and debug it. It's a bit more effort, but it really helps figuring out what's going wrong under the hood.

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