ASP.NET MVC Routing doesn't affect the changes - asp.net-mvc-3

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 ??

Related

NServiceBus and ApiController

i try to configure my NServiceBus for a WebApi. I've tried this one: https://coderkarl.wordpress.com/2012/03/16/injecting-nservicebus-into-asp-net-webapi/
The Problem is the Syntax has been changed in the newest NServiceBus-Versin. I can't use the Functions for the Configure-Class because they will be removed in further Versions. The new way to configure the Bus is using the BusConfiguration-Class but i have no idea how.
Here is the older Code:
public static Configure ForWebApi(this Configure configure)
{
// Register our http controller activator with NSB
configure.Configurer.RegisterSingleton(typeof(IHttpControllerActivator),
new NSBHttpControllerActivator());
// Find every http controller class so that we can register it
var controllers = Configure.TypesToScan
.Where(t => typeof(IHttpController).IsAssignableFrom(t));
// Register each http controller class with the NServiceBus container
foreach (Type type in controllers)
configure.Configurer.ConfigureComponent(type, ComponentCallModelEnum.Singlecall);
// Set the WebApi dependency resolver to use our resolver
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NServiceBusResolverAdapter(configure.Builder));
// Required by the fluent configuration semantics
return configure;
}
And Application_Start():
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
Configure.WithWeb()
.DefaultBuilder()
.ForWebApi() // <------ here is the line that registers it
.Log4Net()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(false)
.PurgeOnStartup(false)
.UnicastBus()
.ImpersonateSender(false)
.CreateBus()
.Start();
Does someone has managed it for the NServiceBus Version 5?
As wlabaj says, the documentation on the particular website says it all. Almost.
We use AutoFac so we don't need any direct reference to IBus or ISendOnlyBus and therefor we do this
ContainerBuilder builder = new ContainerBuilder();
var container = builder.Build();
configuration.UseContainer<AutofacBuilder>(x => x.ExistingLifetimeScope(container));
What we do in WebAPI and ASP.NET applications is this
NServiceBus.Bus.CreateSendOnly(configuration);
Because it's not a good practice to expect reply messages to come back after sending them.
Here you can see 3.0 vs 4.0 vs 5.0 configuration syntax. At the top of the page you have a link to download code samples.
The examples are for ASP .NET though, so you'll need to tweak it slightly for WebAPI. Let me know if you need further help with that.
ForWebApi was never a part of NServiceBus, this was an extension method from the sample that was used to configure NServiceBus dependency resolver to instantiate controllers. The way how it was done is shown here.
There is no need to use NServiceBus resolver since it is just a wrapper around another container. By default it uses Autofac, so you can just use Autofac to work for you in the whole application.
Autofac WebAPI integration is properly described in the documentation.
NServiceBus documentation has a page about using your own container.
This is a very well known setup that you can easily implement.

Cannot access ControllerBase.ValueProvider

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).

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.

Testing If a class is being activated using WebActivator and if add a IModelBinder to ModelBinderProviders.BinderProviders.

Dear fellows from Stack Exchange.
I'm trying to test if my Custom Model Binder is being added to the ModelBinderProviders.BinderProviders collection.
I decided to activate this through WebActivator, to avoid messing global.asax,
Everything works fine, but the Test:
I tried using the WebActivator.ActivationManager.Run() method, but my things weren't loaded.
I've something like this in my test:
[TestMethod]
public void TemplateModelBinderProvider_Should_Be_Registered_In_BinderProviders()
{
WebActivator.ActivationManager.Run();
IModelBinderProvider templateModelBinderProvider = ModelBinderProviders.BinderProviders.
Where(x => x is TemplateModelBinderProvider).
FirstOrDefault();
Assert.IsNotNull(templateModelBinderProvider);
}
And this is my app_Start class:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MVC.App_Start.MVCBindings), "Start")]
namespace MVC.App_Start
{
public static class MVCBindings
{
public static void Start()
{
ModelBinderProviders.BinderProviders.Add(new TemplateModelBinderProvider());
}
}
}
Sorry you have problems with the piece of code I wrote.
I don't have access to the source code right now but will take a look in the evening (UK time).
Do you think you could send me your solution so I could replicate it locally? My email is jkonecki at gmail.com
UPDATE
I have received your source code but unfortunately it contains references to libraries I cannot obtain so I cannot compile it.
I have created a separate solution (emailed to you) with MVC3 web app and unit test projects that uses your custom model binder provide. There are two tests that prove that WebActivatorManager.Run method properly registers a custom provider.
Try debugging your unit test to make sure that Run method calls your static Start method.
WebActivator source code is here - you might want to get it and step through.

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