I am actually successfully running a production website with ASP.NET MVC under Mono in a Ubuntu LTS Linux. This server is located somewhere in the cloud and it rocks!
Today, I have tried to make it all work with ASP.NET MVC 2 Preview 2, which can be found here: ASP.NET MVC 2 Preview 2
I have downloaded the source code because I wanted to deploy the DLL by hand just like I am doing with v1. I find it simpler Linux not to rely on the GAC.
Everything seems to be working fine until I try to get JSON data in an AJAX call. Here is the information I get about the error...
[System.NotImplementedException]: The
requested feature is not implemented.
at
System.Web.Mvc.AssociatedMetadataProvider.GetTypeDescriptor
(System.Type type) [0x00000] at
System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForType
(System.Func`1 modelAccessor,
System.Type modelType) [0x00000] at
System.Web.Mvc.ControllerActionInvoker.GetParameterValue
(System.Web.Mvc.ControllerContext
controllerContext,
System.Web.Mvc.ParameterDescriptor
parameterDescriptor) [0x00000] at
System.Web.Mvc.ControllerActionInvoker.GetParameterValues
(System.Web.Mvc.ControllerContext
controllerContext,
System.Web.Mvc.ActionDescriptor
actionDescriptor) [0x00000] at
System.Web.Mvc.ControllerActionInvoker.InvokeAction
(System.Web.Mvc.ControllerContext
controllerContext, System.String
actionName) [0x00000]
The feature your trying to use is not implemented. The fix would be to actually implement the feature your looking for.
Wait...
Are you using return type of JsonResult instead of ActionResult for your controller actions? Something is trying to populate the meta data about the model your passing to the view. I think if your returning JSON that piece of functionality, the metadata population, shouldn't happen.
Related
I am using the latest version of Sitecore - 7.2 (rev. 140228) and the latest version of WFFM - 2.4 rev. 140923. So the problem is whenever I try to submit a form no matter what kind of save action I have, Sitecore throws exception:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Sitecore.Forms.Mvc.Controllers.ModelBinders.FormModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +571
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +457
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +641
Some additional information. The latest version of WFFM supports MVC so in this scenario I use cshtml layout. This is the blog post where is described how to use MVC - THIS Despite it is for previous versions of SC it doesn't matter and the solution is applicable for the latest version.
Think I have a solution for this.
I reverse engineered and debugged using a version of the controller and model binder I created after reflecting on the Sitecore.Forms.MVC classes using ILSpy.
Apparently the null reference is occuring when it hits this:
if (controllerContext.HttpContext.Request.RequestType == "POST" && formId != item.ID)
item is null. item is set in the line before:
Sitecore.Data.Items.Item item = RenderingContext.CurrentOrNull.PageContext.Database.GetItem(RenderingContext.CurrentOrNull.Rendering.DataSource);
The data source property is empty on the rendering. If you check your presentation details, you'll see that's because there is another field that is used to specify your form ID. Apparently this looks like a Sitecore bug. But an easy workaround is to set the form data source in both the form ID and the Data source property of the rendering.
I'm currently running a simple peace of code that renders a partial view and adds the result to a ConcurrentBag:
Threading.Tasks.Parallel.For(0, Model.Count,
Sub(i)
carXmls.Add(Html.Partial("PublisherVehicleXml16", Model(i)))
End Sub)
The sad thing is that every once in a while this thing crashes with the following exception: Stack empty.
This happens intermittently and only on our staging and production environments. Any ideas where this might be coming from?
Edit: carsXmls is declared like this:
Dim carXmls As New Collections.Concurrent.ConcurrentBag(Of MvcHtmlString)
and the full exception is:
Message Stack empty.
Source System
Target site T Pop()
Stack trace
at System.Collections.Generic.Stack1.Pop() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) at
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.b__19()
at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter
filter, ResultExecutingContext preContext, Func1 continuation) at
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext
controllerContext, IList`1 filters, ActionResult actionResult) at
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName)
You are running code on multiple threads which is using ASP.NET objects. ASP.NET (MVC) does not guarantee you that this is safe. And as you can see, it is unsafe.
In short, you cannot use the ASP.NET infrastructure concurrently.
I suggest that you extract all heavy computations so that you can paralleled them without using ASP.NET objects. You can then render the views sequentially.
I want to localize my error messages on my model using attributes, such as RequiredAttribute and RangeAttribute. I'm using ASP.NET MVC2 in Visual Studio 2010.
This is really easy if my localized resources were in resx resource files... using ErrorMessageResourceName and ErrorMessageResourceType... however, I need to integrate it against a custom localization framework.
I have set the resourceProviderFactoryType on the globalization section in web.config, but MVC doesn't use this. It tries to access a static property on a resource file which returns the localized string.
This is the call stack...
System.ComponentModel.DataAnnotations.ValidationAttribute.SetResourceAccessorByPropertyLookup() +56576
System.ComponentModel.DataAnnotations.ValidationAttribute.SetupResourceAccessor() +146
System.ComponentModel.DataAnnotations.ValidationAttribute.get_ErrorMessageString() +12
System.ComponentModel.DataAnnotations.ValidationAttribute.FormatErrorMessage(String name) +33
System.Web.Mvc.DataAnnotationsModelValidator.get_ErrorMessage() +31
System.Web.Mvc.RequiredAttributeAdapter.GetClientValidationRules() +46
System.Web.Mvc.Html.ValidationExtensions.<ApplyFieldValidationMetadata>b__0(ModelValidator v) +10
System.Linq.<SelectManyIterator>d__14`2.MoveNext() +238
System.Web.Mvc.Html.ValidationExtensions.ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String modelName) +207
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String expression, String validationMessage, IDictionary`2 htmlAttributes) +527
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression, String validationMessage, IDictionary`2 htmlAttributes) +82
System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +75
Does anyone know if it's possible for me to somehow override how MVC retrieves the error message to display?
thanks
k
I would create a custom class attribute that inherits from the standard Mvc HandleError class attribute...
http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html
Does this answer help?
How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution
I have an Asp.Net MVC application that works in the vs.net development web server. When I publish to IIS7 I get the following error. I have spent many hours and still no solution!
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineStepManager.ResumeSteps(Exception error) +929
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +91
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +508
Here is the Application_Start
protected void Application_Start()
{
ConfigureLogging();
ComponentRegistrar.RegisterComponents();
NHibernateSession.InitSqlServer2005(new WebSessionStorage(this), Settings.Default.DefaultConnString);
CacheManager.InitCaches();
}
}
I came late to this application and do not know the best practices of MVC and NHibernate
You cant configure nhibernate in application start. I don't exactly know why, but I also had this problem.
You can initialize it in Init(). Also you can see it is done here
http://code.google.com/p/sharp-architecture/source/checkout
Moving my nhibernate initialization code from Application_Start() to Init() still did not run late enough to fix the error. So I stumbled upon this. The solution I used was not from the original post, but the from first comment by jbland. Basically it moves initialization of nhibernate to occur on the first request.
One thing to note is his code does not give you the context of where webSessionStorage comes from. It is a member variable and must be instantiated in Init().
I am developing an application using MVC Preview 5. I have used typed views.
After setting debug="false", I have found that I am receiving errors of the form:
"The model item passed into the
dictionary is of type
'blah.Models.UserAdmin.IndexData' but
this dictionary requires a model item
of type
'blah.Models.OrganisationAdmin.IndexData'".
I do not get these errors when debug is on.
It looks like that the MVC framework is caching views by view name. If I have two actions that use views with the same name (albeit in different namespaces), I find that only the action that is executed first is successful, the second always results in this error.
For example, I have a UserAdminController and an OrganisationAdminController.
Both have an ‘Index’ action.
Both use a view called ‘Index.aspx’ (each contained in the controller’s view folder; Views/UserAdmin/Index.aspx and Views/OrganisationAdmin.Index.aspx).
Both views are typed and make use of models called IndexData (blah.Models.UserAdmin.IndexData and blah.Models.OrganisationAdmin.IndexData)
If I visit OrganisationAdmin/Index first, I find that any subsequent attempt to view UserAdmin/Index results in the error message shown above.
Conversely, if I visit UserAdmin/Index first (after restarting the application), I find that navigating to OrganisationAdmin/Index causes an equivalent error (with the types the other way around).
I renamed one of my views “UserAdminIndex.aspx” and this seemed to fix the problem. However, this doesn’t feel like it should be an issue. Surely the MVC framework support similarly named views? I am missing something?
Any help gratefully received.
Sandy
Please note, I have seen the question "In ASP.NET MVC I encounter an incorrect type error when rendering a user control with the correct typed object". I am facing a similar problem, but I am not using RenderUserControl().
The stack trace:
InvalidOperationException: The model item passed into the dictionary is of type 'blah.Models.RoleAdmin.IndexData' but this dictionary requires a model item of type 'blah.Models.UserAdmin.IndexData'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +231
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary viewDataDictionary) +99
System.Web.Mvc.ViewPage`1.SetViewData(ViewDataDictionary viewData) +60
System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +64
System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer) +85
System.Web.Mvc.ViewResult.ExecuteResult(ControllerContext context) +206
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ActionResult actionResult) +19
System.Web.Mvc.<>c__DisplayClass12.<InvokeActionResultWithFilters>b__f() +18
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +20
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +20
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ActionResult actionResult, IList`1 filters) +188
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +386
System.Web.Mvc.Controller.ExecuteCore() +112
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +107
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +39
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
This error was driving me nuts. The Model was being specified at the top of my view. Yet the damn thing was requesting a different model.
Turns out there was an orphan ModelType reference in what had become a Layout page. It was being referenced in my view as Layout = "~/Views/_layout.vbhtml"
Yes, the bug has been reported. Best leave debug on for now, or modify the offending code (sorry cant recall where).
I Think your usercontrol and view have same name.