我要提前道歉。我是ASP.NET和MVC的新手,还没有了解内部和操作流程。我正在尝试将ASP.NET MVC Membershp Starter工具包集成到我正在开发的网站中以获得OpenID支持。
我已经解决了几个问题,但是我很难在哪里开始寻找我一直在寻找的空引用异常。这是流程:
呈现此表单时抛出空引用异常。具体来说,抛出异常的部分是:
<% 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>
<% } %>
对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
为空,请求方法为"POST"
。
我从哪里开始寻找问题?看起来它可能与路由有关,但我不确定。
答案 0 :(得分:1)
由于我对这个问题的反应很少,所以我开始寻找将OpenID集成到ASP.NET MVC的替代方案。我发现RPXLib是RPXNow的一个很好的.NET和MVC包装器。这是一种来自多个供应商的OpenID登录的Web服务方法。
这不是一个完美的解决方案,因为我必须在openid凭证和本地配置文件之间编写映射代码,但这足以满足我的需求。
我仍然有兴趣解决Html.Form("controller", "action")
抛出空引用异常的原始问题。我尝试使用具有相同结果的通用版本:Html.Form<Controller>(c => c.Action())
。
答案 1 :(得分:1)
您使用的是ASP.NET MVC v1.0(不是预览版,测试版或RC版)吗?似乎v1.0中没有Html.Form,只有Hrml.BeginForm。如果你看一下ASP.NET MVC v1.0源代码,那么没有Form方法,只有System.Web.Mvc.Html.FormExtensions类中的BeginForm
答案 2 :(得分:1)
如果所有其他方法都失败了,您仍然可以下载ASP.NET MVC source,将其连接到您的项目并进行调试。这需要更多的努力,但它确实有助于弄清楚底下出了什么问题。
答案 3 :(得分:1)
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 Membership Starter Kit Preview 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 there。