MVC HtmlHelpers: can you write in the page title?

I am a web form programmer who is interested in learning a little about MVC. I created a whole set of web controls that can directly access the page title and write things there. For example, if I have a control to render a text field with the datepicker function, it can access the page title to automatically add links to the JavaScript and CSS files it needs. I like it because I'm too lazy to think about which related files I need. Lazy programmer is a good programmer, right?

My question is, is there a way to do this in MVC? That is, to create a custom HtmlHelper (for example), which, as well as rendering control markup on the page, can display the script tags and link that are required in the page header?

+4
source share
4 answers

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

You can then use named sections in your views.

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

With required: false this means that MVC3 will not be an error if there is no named section for the title in the view. If you want ALL pages to have a named section (e.g. bread crums), you can use required: true (which is the default), and if there is no @section Header in the view, this will lead to an error.

+4
source

In web forms, do you have custom server controls that receive and work with the page object? Asp.net mvc does not have server controls or a Page object.

As for me, it is a bad idea to link resources in this way - in the best optimization of client optimization - open the least number of server requests for resources. You can download YSlow to improve the performance of your site.

But if you want to create an extension method and use it in the view or create an actionfilterattribute that sets some URL strings,

0
source

Yes, Telerik does a great job with the ScriptRegistrar and Stylesheet . A good starting point is web resource documentation

0
source

Type script manager

I found this resource on the Internet, which is partially related to your problem and may be the biggest thing that can be done in this regard.

The solution does not really appear in the HEAD element, but it helps to add scripts only once from partial views and display them in the main view. Therefore, you don’t have to worry about your partial viewing scenarios. They take care of themselves (being more encapsulated)

Simple ScriptManager for ASP.NET MVC

User approach to the viewing engine

But otherwise, I believe that this may be possible if you save the links in the dictionary (similar to how the top channel works), and then create your own viewing engine, which displays the head at the end of the script rendering.

User approach ViewPage class

You can also write your winning classes for views and partial views. Thus, you can change the way the view is displayed, which will again use some kind of dictionary. You can fill out this dictionary using the Html helper extension extension method or create your own ViewUserControl class that will have this functionality.

Of the three, I believe that the latter is the simplest and can write inside the HEAD element. You can also provide functionality in it that combines resources.

-1
source

Source: https://habr.com/ru/post/1344265/


All Articles