From web forms to Razor - asp.net-mvc-3

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

Related

How to override the built in templates in MVC Core similar in MVC 4

in ASP MVC (before MVC core like mvc 4,5 ) , we can override the built in templates like string , boolean from EditorTemplates/Boolean.ascx ,the question is :
Is there any way to do it in MVC Core similar to this article https://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html which is applied to MVC 2 ?
It still works the same way, though it was never .ascx. These are Razor views, so they'll need a .cshtml extension. Assuming you add a view like Views\Shared\EditorTemplates\Boolean.cshtml, then all you need to do is #Html.EditorFor(m => m.MyBooleanProp).

ASP.Net Core Web API - ResponseCache attribute is not adding "Cache-Control" header to the reponse

I have multiple controllers in my ASP .NET Core application and I am using ReponseCache attribute like this on a few methods:
//controller
[Route("api/[controller]")]
[EnableCors("CorsPolicy")]
public class InsightsApiController : Controller
//method
[Route("CoursesTextContent")]
[HttpGet]
[DecryptFilter]
[ResponseCache(Duration = 60)]
public IActionResult GetCoursesContent(string locale, string tabKey, string widgetType)
The issue that I am having is that for one controller this is working fine and I can see the response in chrome dev tools with "Cache-Control:public max-age=60" but in a different controller when I add this attribute its adding "Cache-Control:no-cache". I compared both controllers and methods in them and they are configured same. I have also tried to add ASP.NET Core middleware recommended here but same results. I am calling both methods from Angular2 webpage. Is there something I can do from the client side (request)? or something in the ASP.NET Core app setup?
You are missing a trailing parenthesis
[ResponseCache(Duration = 60]
needs to be
[ResponseCache(Duration = 60)]
I had a session middleware enabled in the startup.cs file of my ASNETCore webapi project. I removed it and its working for all calls/controllers now. Not sure why it was causing problem only with one Controller.

VS2013 (RTW): Authentication differences in SPA template vs MVC5 template?

I've been playing with the new ASP.NET identity offerings in the VS2013 RTW MVC template (for "indivual user accounts"), and it works great: I am able to integrate Facebook login while customizing the way the data is serialized.
All well and good, but I noticed that if I create a new SPA app (instead of MVC), the authentication story seems very different. As an example:
From the SPA template:
public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}
public AccountController(UserManager<IdentityUser> userManager,
ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
UserManager = userManager;
AccessTokenFormat = accessTokenFormat;
}
From the MVC template:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
This is just the difference in constructors of the Account controller. There are many, many other differences as well. With the MVC version I was able to easily derive my own context class from ApplicationDBContext, and use that to store my own tables alongside the authentication tables. I couldn't figure out how to customize the data storage in the SPA template.
Also, the SPA template includes and uses this class:
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
The MVC template doesn't define (or use) this class.
I don't understand why there needs to be any differences at all between an MVC template and an SPA template.
Could anyone give me some guidance as to why authentication is handled so differently in these two templates? Starting a project from scratch, is there a preferred path to follow between the two? (It seems like the code in the MVC template is best, especially in terms of customizing how the data is stored by defining a custom EF Context class.)
Thanks...
-Ben
Take MVC and SPA project templates as Controller vs ApiController implementation sample.
As well as CookieAuthentication and oAuthAuthentication.
MVC uses Controller at the first request as well as all subsequent requests (having request defined Action Methods).
SPA uses Controller at the first request to SPA and all other interactions are handled by ApiController.
MVC uses cookie authentication.
SPA uses oAuth authentication.
Now in real apps, we need to take mix of both. Stating this, you can use the IdentityModel.cs (ApplicationDBContext) and it's customized copy of MVC project in your SPA too.
In oAuth implementation, the token is issued in GrantResourceOwnerCredentials method of ApplicationOAuthProvider. The user verification uses the same database of Identity framework by default. Moreover, oAuth provide authentication check in ApiController. In the sample implementation, oAuth's ResourceOwner flow is provided where user's username and password are verified.
In my opinion, templates are starting point examples.
I did notice the same thing when I first looked at all the posts about changing the model for the user and I couldn't find the model in the SPA template. Of course, the difference as #jd4u pointed out is that one is based on Controller and the other on ApiController.
So, I decided to see what it would take to make the SPA solution use the same Identity Model extension as the MVC template. I created a post that goes through the process that I went through. There is a link at the bottom to download the code from GitHub.

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

RDLC reports with ASP.net MVC 3.0

I'm trying to add business object data source for RDLC report in my MVC 3.0 application.However, it couldn't allow me to add object data source to my application.
How I add object data source to my RDLC report ?
Just add an aspx page with all the functionalities you need as you would in a standard WebForms application. You will just need to add a route to it. Here is an example that will probably suite your needs:
public static void RegisterRoutes(RouteCollection routes)
{
// ...
// Some MVC routes
// ...
//Custom route for reports
routes.MapPageRoute(
"ReportRoute", // Route name
"Reports/{reportname}", // URL
"~/Reports/{reportname}.aspx" // File
);
}
The ReportViewer component is not intended to be used with ASP.NET MVC and is unsupported. You may checkout this example. You could have a standard WebForms page (not MVC) generate the report and then include it either with iframe inside your MVC application or have it generate the report as PDF which then could be embedded.
Then check this blog:
asp-net-mvc-handling-ssrs-reports
Handling SSRS Report in ASP.Net MVC 3
Hope this helps.
Regards

Resources