Does ASP.NET MVC come with what is equivalent to the ASP classic server side?
I've been developing in classic ASP for about 2.5 years now and I'm trying to update my skillset to include ASP.NET MVC.
What is the MVC way to do SSI? IE: How to enable navbar sidebar database? I've looked into partial views, but they seem to get their content from the controller. As far as I can tell, this means I will need to write each controller in order to pass the navigation list.
Am I thinking about the correct lines?
a source to share
You have two options similar to SSI
- Use either RenderAction or Action ( explanation of the difference between the two )
- If you are using MVC2, there is also a DisplayFor helper method that will allow you to define a custom display template for a specific object.
In your case, option 1 is probably better as you probably don't want to contain menu items in your data view / view model.
a source to share
As an alternative to using RenderAction () (which has some drawbacks as it has to run the entire ASP.NET request pipeline to get its output), you can use the BaseController type that all your controllers inherit, which overrides OnActionExecuted () to insert values into the ViewData collection. With this approach, you can take advantage of a single query without having to worry about manually adding cut-to-length data to your model each time you process a query.
To keep things simple, I would like to use public const string SomeDataItemViewDataKey = "Controller.DataName";
in the controller class definition to enter ViewData record added by the controller, and then in the presentation, where I need to display this conclusion, I can use the template helpers to pull value from ViewData: <%=Html.DisplayFor(ControllerType.SomeDataItemViewDataKey, "PartialViewUsedToRenderTheData") %>
.
Update
Since there has been some confusion as to the validity of my statement, here is the original source of performance requirements for RenderAction ():
Yes, there is a significant difference in the performance of RenderAction (slower) and RenderPartial (faster). RenderAction, by definition, runs the entire ASP.NET pipeline to handle what appears to the system to be a new HTTP request, whereas RenderPartial simply adds additional content to the existing view.
-Brad Wilson, Senior Developer, ASP.NET MVC Team
Quote source: http://forums.asp.net/p/1502235/3556774.aspx#3556590
Brad Wilson's blog: http://bradwilson.typepad.com/
Update 2
Here is the code from RenderAction () in the RTM MVC2 sources where we can see that although there is a new request that gets fired, it no longer goes through the entire ASP.NET pipeline. However, there are still some minor, though mostly minor, downsides to using it versus the PartialViews and ViewModel / ViewData alternatives. From what I understand (now), there was an overhaul of the RenderAction () implementation before it was moved from the MvcFutures assembly to the main framework; so he can argue that the expression above from Brad Wilson was fairer when he did it 6 months ago than it is now.
internal static void ActionHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, TextWriter textWriter) {
if (htmlHelper == null) {
throw new ArgumentNullException("htmlHelper");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
routeValues = MergeDictionaries(routeValues, htmlHelper.ViewContext.RouteData.Values);
routeValues["action"] = actionName;
if (!String.IsNullOrEmpty(controllerName)) {
routeValues["controller"] = controllerName;
}
bool usingAreas;
VirtualPathData vpd = htmlHelper.RouteCollection.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, null /* name */, routeValues, out usingAreas);
if (vpd == null) {
throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
}
if (usingAreas) {
routeValues.Remove("area");
}
RouteData routeData = CreateRouteData(vpd.Route, routeValues, vpd.DataTokens, htmlHelper.ViewContext);
HttpContextBase httpContext = htmlHelper.ViewContext.HttpContext;
RequestContext requestContext = new RequestContext(httpContext, routeData);
ChildActionMvcHandler handler = new ChildActionMvcHandler(requestContext);
httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), textWriter, true /* preserveForm */);
}
a source to share