开发者

MVC HtmlHelpers: can you write into the page header?

开发者 https://www.devze.com 2023-02-18 11:01 出处:网络
I\'m a web forms programmer who\'s interested in learning a bit about MVC. I have created a whole suite of web controls which can access the page header directly and write stuff in there. For example,

I'm a web forms programmer who's interested in learning a bit about MVC. I have created a whole suite of web controls which can access the page header directly and write stuff in there. For example, if I have a control to render a text box with datepicker functionality, it can access the page header in order to automatically add links to the JavaScript and CSS files it needs. I love this, because I'm too lazy to think about what linked files I need. A lazy programmer is a good programmer, right?

My questio开发者_开发百科n is, is there a way to do this in MVC? That is, to create a custom HtmlHelper (for example) which, as well as rendering the control markup on the page, can render the script and link tags it requires into the page header?


With MVC3 in your _Layout.cshtml (basically master pages) you use @Render.Partial("Header", required: false)

Then you can use named sections in your views.

@section Header {
    @{ Html.MyHelper.GetResources(); }
}

@Html.MyHelper.DoSomething()

With required: false this means MVC3 won't error if the view doesn't have a named section for Header. If you want to require ALL pages have a named section (for example bread crums) you can use required: true (which is the default) and if a view doesn't have the @section Header it would error.


In web forms you have custom server controls, which get page object and work with it? Asp.net mvc does not have server controls and Page object.

As for me it's bad idea to link resources such way - in client optimization best practice - open the least number of requests to the server for resources. You could download YSlow to improve perfomance of your site.

But if your want you could create extension method and use it in view or create actionfilterattribute, which set some url strings;


Yes, - Telerik does a great job of doing this with its ScriptRegistrar and Stylesheet registrar. A good starting point is the web asset documentation


A sort of script manager

I've found this resource on the internet that's partially related to your problem and may be the most that can be done in this regard.

The solution doesn't really render in the HEAD element but it helps adding scripts only once from within partial views and rendering them in the main view. So you wouldn't have to worry about your partial view scripts. They'd take care of themselves (being more encapsulated that is)

A Simple ScriptManager for ASP.NET MVC

Custom view engine approach

But otherwise I suppose it could be possible by saving references in a dictionary (similar to how the upper link does) and then have your own view engine that renders head part in the end rendering script references.

Custom ViewPage class approach

You could as well write your won classes for views and partial views. This way you could change the way that a view renders which would again use some sort of dictionary. You could fill that dictionary via Html helper extension method or create a custom ViewUserControl class that would have this kind of functionality.

Of the three I suppose the last one is the simplest and could write inside HEAD element. You could also provide functionality in it that would do resource combining.

0

精彩评论

暂无评论...
验证码 换一张
取 消