Showing posts with label asp.net mvc. Show all posts
Showing posts with label asp.net mvc. Show all posts

Tuesday, June 23, 2009

Test asp.net mvc route

//arrange RouteCollection routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Expect(c => c.Request.AppRelativeCurrentExecutionFilePath).Return("~/product/list"); //act RouteData routeData = routes.GetRouteData(httpContextMock.Object); //assert Assert.IsNotNull(routeData, "Should have found the route"); Assert.AreEqual("product", routeData.Value["Controller"]); Assert.AreEqual("list", routeData.Value["action"]); Assert.AreEqual("", routeData.Values["id"]);

Thursday, June 11, 2009

mvc routing

Routing is actually not part of the asp.net mvc component. But mvc depends on this asp.net components. To add a route to the route table, you can use the following code.

Route r = new Route("url", new SomeRouteHandler()); r.Constraints.Add("key", "value"); r.Defaults.Add("key", "value"); r.DataTokens.Add("key", "value"); RouteTable.Routes.Add("route_name", r); // or you can write the code in .net 3.0 syntax, they do the same job //but it looks cleaner. RouteTable.Routes.Add(new Route("url", new SomeRouteHandler()) { Constraints = new RouteValueDictionary(new { key = value }), Defaults = new RouteValueDictionary(new { key = value }), DataTokens = new RouteValueDictionary(new { key = value }), });

asp.net mvc, add some extension method to the RouteCollection like the following.

public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces) { if (routes == null) { throw new ArgumentNullException("routes"); } if (url == null) { throw new ArgumentNullException("url"); } Route route = new Route(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; if ((namespaces != null) && (namespaces.Length > 0)) { route.DataTokens = new RouteValueDictionary(); route.DataTokens["Namespaces"] = namespaces; } routes.Add(name, route); return route; }

This method create a route use MvcRouteHandler as IRouteHandler. So if use this method, the MVC component comes into play. So you can these extension method to simplify the mvc routing

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults );

The default values only work when every URL parameters after the one with default also has a default value assigned. So the following code doesn't work.

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home" } // Parameter defaults );

The following code demo a catch-all parameter

routes.MapRoute("r2", "catch/{*all}", new { controller = "test", action = "catch", all="empty" }); public string Catch(string all) { return string.Format("<h1>all:{0}</h1>", all); }

Friday, January 9, 2009

The order of route is important in asp.net mvc

I have try to add route to reroute the default.aspx to home handler.

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); routes.MapRoute("default.aspx", "default.aspx", new { controller = "Home", action = "Index", id = "" });

It throw an exception "The controller for path '/default.aspx' could not be found or it does not implement IController.". Why, it turns out the order of adding route is important. I should put my routing of "default.aspx" on top of "{controller}/{action}/{id}".

Friday, October 31, 2008

View is the king.

ASP.NET MVC is very hot today, it catches more and more attention from asp.net developer. There are many problems it tries to solve, like testability, separation of concern ect. But to me the most important think it solves is the View. There are not many web 2.0 web site use asp.net to develop, but PHP is very widely used for these web site? Why? There are many reasons, but the most important reason is the View is back.

In session PC21 ASP.NET MVC: A New Framework for Building Web Applications of latest PDC 2008, an audience asked Haack, "do we need to known html?". What kind of question is that? When you love somebody, you can give of a long list of the merits of her or him. I love asp.net mvc, because it is testable, separation of concern, inversion of control. But to me, it think the force of driving mvc is the view. ASP.NET MVC put the view to the control of developer and designer. Naive developers love the server control, in fact they are spoiled to a degree to ask the question of "Do we need to know html". There is nothing wrong with from this practice, in fact asp.net page model and server control framework does encapsulate lots of html, javascript. But developers are quite limited to what the server control has to offer. Although server control is very extensible, developers need to have learn very deep to fully leverage the power of it, until we understand internal of viewstate, render control, javascript, css, we can develop our own server control. I think that is why there are many web 2.0 site is not implemented in asp.net. Now the view is back, we can fully control how the view is rendered.