Routing in a hybrid app that is classic ASP.Net web forms and MVC - asp.net

i have inherited an application that is both clasic asp.net web forms and mvc (started as web forms project). I would like to setup routing like MVC. What is the best way to go here? All i need is a push in the right direction.
I know how to setup routing for the MVC only project via global.asa > App_Start > Route Config and area registration cs files.
Environment:
VS 2012, IIS 7, ASP.NET 4.0, classic asp.net web forms and MVC 4.
My thinking:
I am thinking about doing some thing like following, do you guys see an issue here? I may end up with some web.config issues but at this this time i am not sure what those would be. I need your advice to properly setup the structure here.
Global file addition:
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
App_Start > RouteConfig.cs
namespace My.Site
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "My.Site.Controllers" }
);
OTHER ROUTES WILL GO HERE, THESE MAY REDIRECT TO a webform page or to a controller action.
}
}
}

Going through the following to setup all properly.
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc
http://msdn.microsoft.com/en-us/library/dd329551.ASPX

Related

Re-mapping folders in a asp.net web application

I am converting an asp.net webforms website type project to an asp.net mvc web application. I want to keep all the changes to a minimum.
The pages and its subfolders are right at the top of the project folder and there are hundreds of them.
Now, in the new web application, I want to move them to a subfolder, let's call it WebForms.
Is there a way at runtime to run the pages as they ran before, i.e. at the root of the application folder?
Before I had: http://localhost:54321/Page1.aspx. Page1.aspx was stored in the website project root folder.
In the new project structure I have the disk:
<project folder>
WebForms
Page1.aspx
This works: http://localhost:54321/WebForms/Page1.aspx, but I want to somehow map it to http://localhost:54321/Page1.aspx.
Is it doable? I use IIS Express for development and IIS 7.5 for test/production deployment. I want to avoid having to change the image and other content urls - as you can imagine moving the pages to the subfolder breaks some of them.
Thanks
If you just want to map all requests to /something.aspx so they go instead to WebForms\something.aspx, you can probably just use the following route rule.
routes.MapPageRoute(
"Other Web Pages",
"{pagename}.aspx",
"~/WebForms/{pagename}.aspx");
Alternately, if you need more advanced scenarios, you could use a custom class that derives from RouteBase and use a RegEx to match for and map the route, similar to this question's answer
public class WebFormsRoute : RouteBase
{
Regex re = new Regex(#"^/(?<page>\w+)\.aspx", RegexOptions.IgnoreCase);
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = new RouteData();
var url = httpContext.Request.FilePath;
if (!re.IsMatch(url))
{
return null;
}
var m = re.Match(url);
data.RouteHandler = new PageRouteHandler("~/WebForms/" + m.Groups["page"].Value + ".aspx");
return data;
}
}
And then add it to your route collection in RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new WebFormsRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Can you use the attribute-based routing of WebApi 2 with WebForms?

As the title states, I'm wondering if you can use the attribute-based routing of WebAPI 2 with WebForms. I feel like this can obviously be done given you can use WebAPI2 just fine in a WebForms application... I just can't figure out how to enable attribute-based routing.
Based on this article, I understand you normally enable it via a call to MapHttpAttributeRoutes() prior to setting up your convention-based routes. But I'm guessing this is the MVC way - I need to know the equivalent for WebForms.
I currently use MapHttpRoute() to set up my convention-based routes, and I'd like to try out the attribute-based routing in WebAPI2. I have updated my project with WebAPI2 - I just need to know how to enable the attribute-based routing feature.
Any info would be appreciated.
You need not do anything special in case of WebForms. Web API attribute routing should work just as in MVC.
If you are using VS 2013, you can test this easily by create a project using "Web Forms" template and then choose "Web API" check box and you should see all the following code generated by this.
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Global.asax
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
WebForm's RouteConfig
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}

URL Rewriting in Asp.net MVC 3

I am new to Asp.net MVC.
I am creating web application, where i have to rewrite url with product name.
I am not sure if that is possible or not in MVC.
Like,
http://sitename.com/category1/product1
http://sitename.com/category1/product2
will have same page.
There are facilities to generate friendly urls within MVC.
Check out the article at - http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs - for an overview of how this is handled in MVC.
Essentially, you need to configure the routes on application startup as follows. This can usually be done in the global.asax file but cna be split for areas etc.
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 = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes); // Reigster the routes
}
This is the default route but you can define as required.

default page issue with 4.0 ASP.NET and MVC mixed mode website

I am converting a classic ASP.NET 4.0 site to also use MVC. Over time I am migrating the ASP.NET code to MVC, but during the transition both technologies will be in use.
If I navigate to the default page (ie, http://mywebsite.com/), then MVC routing is taking over and returning the following message.
This request has been blocked because sensitive information could be
disclosed to third party web sites when this is used in a GET request.
To allow GET requests, set JsonRequestBehavior to AllowGet
If I use http://mywebsite.com/default.aspx, then everything works fine.
My route config looks like...
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Notice I am ignoring .aspx page requests, so these requests get ignored by the MVC pipeline. However, I need 'no page specified' default requests to process default.aspx. How would I change the above code or configure the site/IIS to make this happen?
I removed the default route and now the behavior is as needed.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}

Receiving 404 error when trying to get to MVC application in local IIS

I have an existing web site setup on my local IIS built with web forms setup in this format:
-MainSite
-Application1
-Application2
-Application3
There are separate applications in each location, so MainSite has an application, and Application1 has another application and so on. This way development on different applications in the same site can be broken out. I want to add a new Application that is MVC instead of web forms. I created a new MVC project which runs in Cassini just fine, but when I create a new IIS application for it and point it to the MVC project I am unable to navigate to it in the browser and get a 404 error. I am expecting to be able to navigate to http://MainSite/NewMVCApplication/. I want the root of NewMVCApplication to point to my Contacts Controller.
My global.asax in the new MVC application currently looks like this:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Contacts", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
}
Make sure NewMVCApplication is running in an integratedMode pipeline application pool.

Resources