I have a hybrid ASP.NET MVC application, and I'd like to test that the route table properly allows my .aspx pages to be loaded. In particular I'm interested in making sure that the root of my site goes to index.aspx, not to a controller.
Update: You can test for the Ignored paths in the routing table. However, this doesn't test for the route on the root if you haven't provided a default controller. The routeData comes out null.
MvcContrib has a RouteTestingExtensions class in their TestHelper. This is how I test routes. The tests end up looking like this:
"~/computer-accessories/".ShouldMapTo<CategoryController>(x => x.Accessories());
You could try Phil Haack's route debugger, although what Craig said is correct.
Well that's easy. Root always goes to Default.aspx, even in "pure" MVC. MVC apps have a special Default.aspx to reroute to Home/Index. Yours will be a real page, instead.
Related
The AttributeRouting package contains a handler called LogRoutesHandler that "emits the routes in the RouteTable to a browser".
Is there something similar in the new MVC5 attribute routing functionality?
I also miss the routes.axd feature of AttributeRouting in ASP MVC 5. I am using currently using Glimpse to debug routes. It's not quite the same, far from friendly compared to routes.axd, but it helps.
I find myself having to create a Web Form page in my MVC3 application to host the ReportView control, for displaying rdlc reports for my users. I see quite an abundance of advice on how to go about this all, and it seems pretty simple.
What I would like to know is how do I get from a Razor based view to this report viewer page? Do I simply 'hard-code' a content url to the page, or is there some more polite way?
You can do this by adding route in global.asax file.
routes.MapRoute(
"ReportRoute", // Route name
"Reports/{reportname}", // URL
"~/Reports/{reportname}.aspx" // File
);
Check out LeftyX solution with source code.
You could use the Url.Content helper:
Show me the report
I'm developing an ASP.MVC 3 project on my local computer where it is located at the root of the local web server.
localhost:12345/(project is here)
However, when I deploy to our public web server this application will be located in a subfolder
www.mycompany.com/myapp/(project goes here)
How do I deal with that mismatch? A few questions come to mind:
Do I need to adjust my MVC routes? Or will they just capture anything after /myapp/ ?
Do I use HomeController when I don't really want 'Home' to appear in the route? i.e. /myapp/home/(action)/(id) - rather I want this: /myapp/(action)/(id) if Home is the controller.
Should I match this folder structure on my local machine? (This project will never have access to anything outside that 'myapp' folder)
If 3 is yes, how?
I'm using VS 2010 with IIS Express locally.
The routes are relative to the web application. Which means that you don't need to have /myapp/ in your routes.
No you don't need to have Home appear in the route. Personally as a practice I take out the default route.
No you don't need to match this folder structure.
In our experience I found that within the Controllers there is no problem with the routes.
However, if you use HTML helpers within your views, like Html.BeginForm or Html.Action, these do not work with the overloaded methods that receive controller, action arguments. You need to put the whole Url in the Html helpers using Url.Content,
This does not work:
Html.BeginForm()
or
Html.BeginForm("ResetPassword", "Account")
But this works:
Html.BeginForm(Url.Content("~/Account/ResetPassword");
i'm using asp.net mvc with vs2008 and IIS7.
What i want to accomplish is that all requests that START WITH 'summer' are routed to the same controller.
'till now i've built tons of routes, but they were all for one path (with parameters offcourse) but this one must route:
www.mysite.com/summercity
www.mysite.com/summermadness
www.mysite.com/summer
www.mysite.com/summerweather
to the same controller.
the only solution i've come up with is 'summer*', but that didn't work :)
Michel
You can use:
/Summer{*Data}
I'm working with an ASP.NET MVC site which will use a CMS controller for all pages of the site except for the home page. Here's the idea:
Home controller:
www.site.com
www.site.com/default.aspx
CMS Controller:
www.site.com/about
www.site.com/agenda/schedule
www.site.com/monkey/eats/spaghetti
(pretty much anything else)
This page lists some options on how to set up a default page routing:
Leave Default.aspx unrouted and unredirected as the entry point to your application - with static links that take your users into the MVC portion of the app (or other static content).
Redirect Default.aspx in the code behind, either using the Page_Load event handler code, or use Response.Redirect("~/home") to send them to the Home controller (although this is a round-trip redirect).
Rename or delete Default.aspx. Despite the warning in the markup that says that default.aspx is required to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request... it's not actually needed in either the VS dev server, or IIS7. The default request will remain an application root request "/" and will be caught by the default route and sent to the home controller.
I guess one other option is to just use one controller with some logic that detects the home page case, but that seems to be fighting the concept.
How do you recommend setting up a specific route for the site home page?
www.site.com can be handled by an root map route
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
Put the following in page load of Default.aspx
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
This rewrites the request to root and handled by the map route above.
BTW, you can actually find the code from the MVC template project.
If hosting on IIS7 integrated mode, I suggest just getting rid of default.aspx. As I understand it, it's only necessary for activation on IIS6 and IIS7 classic mode.
I think option #1 is easiest. I probably will stick to it until finding a strong reason to move or finding the alternative. The default template uses this approach.
[UPDATE] Canton beat me to it