I have followed all steps on this link including web.config changes and adding required assemblies.
ASP.Net and Webforms in Harmony
I have installed MVC3 into the webforms project and Implemented a controller and registered its routes in Application_Start method of Global.asax.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Here is the controller
public class HomeController : Controller
{
//
// GET: /Default1/
public ActionResult Index(int? id)
{
ViewData["Message"] = "Hello from Home controller";
return View();
}
}
I am trying to call its action (i.e /Home/Index) but getting 404 Not Found error.
Route registered for other .aspx form are working fine.
routes.Add("Home", new Route("Home", new RoutingHandler("/Default.aspx")));
Everything is working fine but (Home/Index) is not showing up.
You can accomplish this by adding Area in your web project for example MVC. Then you need to register that Area in Global class in Global.asax.cs file's function protected void Application_Start(object sender, EventArgs e) function like below
MVCAreaRegistration.RegisterAllAreas(RouteTable.Routes);
Now add a controller for example Home and add a view say Index. Place a break point on Index action method and run your application. In url type ".../MVC/Home/Index" and the break point will got hit.
Related
I have an existing Web application that was developed in ASP.NET 4.0. I want to add MVC functionality to the app, so I've integrated MVC into the app as per Scott Hanselman's article Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications. Because MVC routing is greedy, I added the following code to my Global.asa so that an empty URL will go to my Default.aspx:
routes.MapPageRoute("WebFormsDefault", "", "~/Default.aspx");
The problem now is that ActionLinks and RouteLinks don't form correctly. If I try to create an action link using:
#Html.ActionLink("Item List Page", "List", "Item")
the following URL is created:
"/SiteName/?action=List&controller=Item
I've found several posts from others with this same problem, but none of them have any answer. Is this just a bug? Is integrating MVC into a WebForms app just a bad idea in general? Or is there a way to fix this so that my Default.aspx page will be displayed when a user first enters the site and ActionLinks and RouteLinks will work correctly?
Coming to this a bit late, but I figure better late than never. I was having this exact same issue and I solved it by grouping my MapPageRoute code and my MapRoute code and then always calling the MapRoute code first. Example:
Originally my routing looked like this -
routes.MapPageRoute("401", "401/", "~/Views/Error/401.aspx");
routes.MapPageRoute("404", "404/", "~/Views/Error/404.aspx");
routes.MapPageRoute("500", "500/", "~/Views/Error/500.aspx");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
etc etc
This was causing all of my form actions to direct to a url formatted as so:
/mysite/401?action=x&controller=y
Clearly that was not useful. By making sure that I always setup all of my MVC routes first, the problem resolved itself. I ended up making two seperate methods, one for configuring MVC routes and one for configuring Webform routes as so:
RouteConfig.RegisterMvcRoutes(RouteTable.Routes); // contains only MapRoute
RouteConfig.RegisterWFRoutes(RouteTable.Routes); // contains only MapPageRoute
(these calls go into the Global.asax file as usual and replace RouteConfig.RegisterRoutes)
I would not advise mixing webforms with MVC, but I did manage to get this working by using this helpful posting:
http://bartwullems.blogspot.com/2011/04/combining-aspnet-webforms-and-aspnet.html
I also had to be rather careful about the ordering of the routes so that my most generic route went after the aspx page I wanted to be served up as a default.
Here is my complete RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute({resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SignUp",
url: "SignUp",
defaults: new { controller = "Profile", action = "SignUp", shortUrl = UrlParameter.Optional });
routes.MapRoute(
name: "Admin",
url: "Admin",
defaults: new { controller = "Admin", action = "Index", shortUrl = UrlParameter.Optional });
//used to get aspx page to render
routes.MapPageRoute("WebForms", "", "~/WebForms/Default.aspx", false, null, new RouteValueDictionary(new { controller = new IncomingRequestConstraint() }));
//this generic route must go last
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
public class IncomingRequestConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return routeDirection == RouteDirection.IncomingRequest;
}
}
I'm currently facing an issue with an MVC application I'm busy with, when testing the site in a local environment it shows the login page like it's supposed to as specified in the RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
But as soon as this gets uploaded the server it only renders the _layout.cshtml.
I have also attempted to specify the page in the application properties but this has not worked either.
Any advice or things to try would be greatly appreciated and thank you in advance.
Please note, when routing to the controller and views manually it all works fine, it's just the initial load of the site.
I don't know what happened to my website. From today the default action "Index" in only one controller doesn't work anymore.
If I call http://website.com/Valuation i get 403 error page because the webserver doesn't route my request and try to browse the folder. If I write http://website.com/Valuation/Index everything works.
I search in all the code but i can't find the problem, everything seems fine like other controllers.
How can i find the problem? Do you know if there are a known issue that cause that problem or you know if there is a trace\log\debugger of routing requests?
Thanks
Mic
Most probably the issue is you have a folder by name Valuation in your website root. Thats why the valuation index action is not working. Instead of routing to the Controller Action the url http://website.com/Valuation is routing to the Folder Valuation.
Delete this folder Valuation from your root or rename it then this url http://website.com/Valuation will work.
Also check if the ValuationController has the public ActionResult Index() ([HttpGet] method.
Check ~\App_Start\RouteConfig.cs file
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}
);
}
}
This is a very strange problem. I am new to the mvc world coming from web forms and i am trying to understand its concepts. Using the MVC template in vs 2013 (premium), I have built a project. In order to see how things work:
I create a new controller 'IndexController' and put it in the folder .../Controllers/IndexController.cs
I create a new View for this controller 'Index.cshtml' and put it in the corresponding folder: .../Views/Index/[#] Index.cshtml
Then I change the routing so that the default routing will now point to this IndexController and not to the default HomeController
Here is my routing table:
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 = "Index", action = "GetIndexPage", id = UrlParameter.Optional }
);
}
}
You can see that I am using 'GetIndexPage' as the default action instead of 'Index' (I'm playing around and see how it works)
Whenerver I make a change in the Index.cshtml (say I add a simple markup and hit 'Run' I always receive the error message
Server Error in '/' Application. The resource cannot be found.
Looking at the address bar I see that the browser is looking for the resource 'localhost:xxx/Index/Index' instead of 'localhost:xxx/Index/GetIndexPage'. To solve this problem, I go in the IndexController and put a breakpoint inthe line
return View(...);
Now I hit 'Run', after stopping at the breakpoint, every thing works perfectly. So it is not a problem of routing since the page is displayed after this breakpoint trick. Visual Studio seems to mess up with the deployment to the IIS Express I am using after I have made a change to the cshtml view. The problem does not occur when I make a change in the code behind of the controller. I don't know where to look at...I have spent this whole night trying to find a solution in Google and stackoverflow...I don't want to reinstall the whole visual studio. Any help, any hint to a certain direction will be greatly appreciated.
After a lot of trials I discover that mvc framework wants the action and the view (that this action returns) must have the same name. In my case this is what has worked:
route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "IndexPage", id = UrlParameter.Optional }
);
IndexController:
public class IndexController : Controller
{
public ActionResult IndexPage()
{
return View();
}
}
You can see that route action has same name with method of controller: IndexPage
And finally create a view with name IndexPage.cshtml
With this configuration when I make a change in the cshtml file it is immediately reflected in the browser and there is not the error reported above.
I just want to have a confirmation if this is indeed the way things must be set up with the mvc approach. (thanks with your help)
I'm trying to route a .aspx (webforms page) in my asp.net mvc project. I register the page in global.asax:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Reports/Tickets.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
The problem is, after i add the second line, the site stops to enter in my Home Controller (Index Action) and is redirecting to: http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22 always that i run the project.
Project Details:
Asp.Net MVC 3
Forms Authentication
.Net 4.0
Obs: to reproduce this error, create a new asp.net mvc project as internet app, after create the Tickets webforms page inside a /WebForms/Reports folder, and register the new route. Run the project (probably you're logged), so now logoff and you will be redirected to http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account, so why?
Solved! So, we need to add a route contraint to the webforms route to ensure that it only catches on incoming routes, not outgoing route generation.
Add the following class to your project (either in a new file or the bottom of global.asax.cs):
public class MyCustomConstraint : IRouteConstraint{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){
return routeDirection == RouteDirection.IncomingRequest;
}
}
Then change the Tickets route to the following:
routes.MapPageRoute(
"Tickets",
"Reports/Tickets",
"~/WebForms/Reports/Tickets.aspx",
true, null,
new RouteValueDictionary { { "outgoing", new MyCustomConstraint() } }
);