ASP.NET MVC - Code Execution Every Time Site.Master Processes

I would like to add a little variation to the path of my css link each time my Site.Master view is processed. What is the right way to do this? My code is currently breaking into Default.aspx, saying that I did not define cssLink. Site.Master below:

    <script runat="server">
        void Page_Load(object sender, EventArgs e)
        {

            string cssLoc = "../../Content/css/expect.css?t=" + DateTime.Now.Ticks.ToString();
            string cssLink = String.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />", cssLoc);
        }
    </script>

    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">   
        <asp:ContentPlaceHolder ID="head" runat="server">
        <title></title>
        </asp:ContentPlaceHolder>    
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <%= cssLink %>
        <script type="text/javascript" src="../../Scripts/jquery.js"></script>
    </head>

Also, is there anyway the correction of the message "XHTML transitional, Title is too few times"?

UPDATE:
Please ignore the problem with scoping. See Richard's answer. However, I must note that fixing this does not solve the problem.
I think because of the declaration Inherits = "System.Web.Mvc.ViewMasterPage" the whole file Block is <script runat="server">not processed.

+1
4

CSS.

public static string DatedStylesheet(this HtmlHelper Html, string url, DateTime date)
{
    UrlHelper Url = new UrlHelper(new RequestContext(Html.ViewContext.HttpContext, Html.ViewContext.RouteData));
    string html = "<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}?t={1}\"/>";

    return string.Format(html, Url.Content(url), date.Ticks.ToString());
}

<%= Html.DatedStylesheet("~/Content/css/expect.css", DateTime.Now);

- UrlHelper ?

+2

css, , . Page_Load MVC, ...

<link href="../../Content/css/expect.css?t=<%=DateTime.Now()%>" type="text/css" rel="Stylesheet" />
+5

cssLink Page_Load. , .

:

<script runat="server">
    private string cssLoc;
    private string cssLink;

    void Page_Load(object sender, EventArgs e)
    {

        cssLoc = "../../Content/css/expect.css?t=" + DateTime.Now.Ticks.ToString();
        cssLink = String.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />", cssLoc);
    }
</script>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">   
    <asp:ContentPlaceHolder ID="head" runat="server">
    <title></title>
    </asp:ContentPlaceHolder>    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <%= cssLink %>
    <script type="text/javascript" src="../../Scripts/jquery.js"></script>
</head>

: , MVC ( , ). , css ViewData [ "Stylesheet" ] css ( RC MVC)

+2
<head runat="server">
      <title>Some Title</title> -- WILL FIX YOUR ISSUE
      ...
      <%= Helper.CustomStyle() %>
      ...
</head>

Helper.CustomStyle , DateTime.Now, .

, Custom Helpers, : http://www.asp.net/learn/mvc/tutorial-09-cs.aspx

- ASP.NET MVC Page_Load. !

+2

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


All Articles