Cannot access ControllerBase.ValueProvider - asp.net-mvc-3

I have been playing around with the XamlAsyncController, which is based on ASP.NET MVC 2, and tried upgrading it to MVC 3. However, although the original application works OK, if I try to run it in MVC 3, I get a NullReferenceException when attempting to access ValueProvider.
I've tried explicitly loading the default value providers in the Application_Start:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ValueProviderFactories.Factories.Add(new RouteDataValueProviderFactory());
ValueProviderFactories.Factories.Add(new FormValueProviderFactory());
ValueProviderFactories.Factories.Add(new HttpFileCollectionValueProviderFactory());
ValueProviderFactories.Factories.Add(new QueryStringValueProviderFactory());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BootstrapContainer();
}
I'm using Castle Windsor 3 to manage IoC. Does anyone have any idea why the ControllerBase.ValueProvider would be null?

In the end I gave up and tried a different project (a view engine that renders XAML as PNGs).

Related

How to modify subject of Elmah emailing when using WebApi

Normally in my Mvc project, I simply have a ErrorMail_Mailing method in Global.asax.cs which gives access to ErrorMailEventArgs.
But in WebApi, this method does not fire, so how can I access this information in Webapi?
I am currently using this methodology (which is working fine)
public class ApiErrorHandler : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
DepResolver.ExceptionHelper().LogToElmah(context.Exception);
base.OnException(context);
}
}
ELMAH doesn't fire on Web API by default. You need to either catch the error yourself and log it to ELMAH or even better, use the Elmah.Contrib.WebApi NuGet package: http://www.nuget.org/packages/Elmah.Contrib.WebApi/. With that package installed, simply add the following code to your Application_Start:
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());
This should trigger that your ErrorMail_Mailing method is called.

ASP.NET MVC Routing doesn't affect the changes

I'm using Asp.Net MVC3 and wanna to define a custom route, I test my route in other project and it works well but in main project it doesn't!
as I made a question before here
it is so interesting that I comment all of my defined route , but it still works as before!
(my project doesn't look to my Routing !)
is it possible to MVC to render any page in this case ??? I have done like this
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// RegisterGlobalFilters(GlobalFilters.Filters);
// RegisterRoutes(RouteTable.Routes);
// Initializer
Database.SetInitializer(new Initializer());
//Ninject
ControllerBuilder.Current.SetControllerFactory(new NinjectDependencyResolver());
}
Notice about this line of code that is comment !
// RegisterRoutes(RouteTable.Routes);
I think something has override my MVC routing that any change in my Routing doesn't affect in My project ,what is the problem with that ??

CustomErrors not redirecting to custom page but MVC default error page

I have a new MVC 4.0 solution created from scratch using VS2012 web express and IIS Express.
As the tile says I changed my web.config and added the following in :
<customErrors defaultRedirect="Errors/GenericError.html" mode="On">
<error statusCode="404" redirect="Errors/Error404.html"/>
</customErrors>
Basically when I get an exception in my controller the default MVC error.cshtml is showing the error instead of my custom page GenericError.html.
If I go to a URL that doesn't exist, my Error404.html is showing correctly but not for the generic scenario.
Any ideas how I can change this behavior?
Sounds like you don't have the error attribute in your global filters. In an MVC 4 project you should be able to search for the class FilterConfig that was generated for you:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
Update:
The HandleErrorAttribute global filter is for errors that occur in the MVC pipeline, such as errors in your controller as you mention.
The customErrors element in the web.config is for everything else. The HandleErrorAttribute will not honor the value you put in the defaultRedirect attribute of customErrors.
The HandleError filter will look for a shared view which you should have in your generated project shared\Error.cshtml. You can point it to a different view by setting a property on the attribute.
For example, let's say we create a new view under Shared\Errors\CustomError.cshtml, then you could register the filter like this:
filters.Add(new HandleErrorAttribute(){View = "Errors/CustomError"});

asp.net mvc - common code to execute when loading any page from the application

Where would such code go? Is there a commonly executed block inside Asp.net mvc 3 application - something that gets executed every time any page is loaded?
You can do this by two ways:
First, you can inherit a base Controller from System.Web.Mvc.Controller. Then you use this base class inherits for your application. By this way, you can handle all action executions by overriding OnActionExecuting method of your base controller.
Second and better solution is using Custom Action Filters. Create a custom filter and register it globally in Global.asax file like this:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new YourCustomFilter());
}
Global.asax (ex: http://www.dotnetcurry.com/ShowArticle.aspx?ID=126) or inside the _Layout, it depends on what you're doing.
Just so you know the Global.asax file is also available in ASP.NET Webforms.

From web forms to Razor

I've got an .net 2.0 web forms site that has just been upgraded to .net 4. Now I'd like to use the Razor syntax and some mvc helpers. Could anyone give a step by step procedure to start using it?
(Yes, I know mixing different view engines is not straight forward, but I'm not asking for that. Just to be able to create a new _layout, and a new ContentPage.cshtml and start using some of the mvc helpers and get that to work in parallell with the old pages - I'll duplicate the masterpage functionality, so that new pages will be written using razor, and old pages bugfixed in webform with the old masterpage)
I just need to know the following:
What assemblies do I need to include
What changes to web.config do I need
Any other changes?
Thanks for any help
Larsi
Scott hanselman has a great post about this:
Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications
You need to include System.Web.Mvc version 3.0.
In your web.config, you need to make sure that the UrlRoutingModule is registered as an HttpModule. Your IHttpHandler is created by the IRouteHandler implementation, which is an MvcRouteHandler in ASP.NET Mvc.
You also will need to register your routes in your Global.asax to setup routing. The default Route registration (for an MVC2 project) looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
I'm not sure if they have made any changes to that in Mvc 3 or not, but you can find out by creating a new Mvc Web Application project in Visual Studio and opening up the Global.asax
You may take a look at the upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 guide. If you have a classic WebForms application (not MVC) then there is no migration => there is a rewrite.
This converter tool will get you a head start:
http://visualstudiogallery.msdn.microsoft.com/d2bfd1ca-9808-417c-b963-eb1ea4896790
Telerik wrote a command-line converter from aspx/ascx to cshtml for asp.net mvc. You can find that at: https://github.com/telerik/razor-converter
There is also a nice plugin for Visual Studio that uses the Telerik code at: http://visualstudiogallery.msdn.microsoft.com/d2bfd1ca-9808-417c-b963-eb1ea4896790

Resources