2012-03-06 73 views
0

我有这样的HTML帮助选择自定义控制CSS文件中的ASP MVC

public static MvcHtmlString EditButton(this HtmlHelper html, string action, 
     string controller, bool state, Themes theme) 
    { 
     var url = new UrlHelper(html.ViewContext.RequestContext); 
     var linkBuilder = new TagBuilder("link"); 
     string path; 

     switch (theme) 
     { 
      case Themes.brown: 
       path = "../../Content/themes/" + Themes.brown .ToString()+ "/style.css"; 
       break; 
      case Themes.darkblue: 
       path = "../../Content/themes/" + Themes.darkblue.ToString() + "/style.css"; 
       break; 
      case Themes.darkorange: 
       path = "../../Content/themes/" + Themes.darkorange.ToString() + "/style.css"; 
       break; 
      case Themes.defaultTheme: 
       path = "../../Content/themes/" + Themes.defaultTheme.ToString() + "/style.css"; 
       break; 
      case Themes.green: 
       path = "../../Content/themes/" + Themes.green.ToString() + "/style.css"; 
       break; 
      case Themes.greyblue: 
       path = "../../Content/themes/" + Themes.greyblue.ToString() + "/style.css"; 
       break; 
      case Themes.lightblue: 
       path = "../../Content/themes/" + Themes.lightblue.ToString() + "/style.css"; 
       break; 
      case Themes.lightorange: 
       path = "../../Content/themes/" + Themes.lightorange.ToString() + "/style.css"; 
       break; 
      case Themes.pink: 
       path = "../../Content/themes/" + Themes.pink.ToString() + "/style.css"; 
       break; 
      case Themes.red: 
       path = "../../Content/themes/" + Themes.red.ToString() + "/style.css"; 
       break; 
      case Themes.yellow: 
       path = "../../Content/themes/" + Themes.yellow.ToString() + "/style.css"; 
       break; 
      default: 
       path = "../../Content/themes/" + Themes.defaultTheme.ToString() + "/style.css"; 
       break; 
     } 
     linkBuilder.MergeAttribute("href", "@Url.Content(" + path + ")"); 
     linkBuilder.MergeAttribute("rel", "stylesheet"); 
     linkBuilder.MergeAttribute("type", "text/css"); 

     //génrer le tag <a> 
     var builder = new TagBuilder("a"); 

     //ajouter les différents attributs du tag 
     builder.MergeAttribute("href", url.Action(action, controller)); 
     builder.MergeAttribute("alt", "edit"); 
     builder.MergeAttribute("title", "Edit"); 



     if (state) 
     { 
      builder.AddCssClass("edit_active"); 
     } 

     else 
     { 
      builder.AddCssClass("edit_inactive"); 
     } 

     string anchorHtml = builder.ToString(TagRenderMode.Normal); 

     return MvcHtmlString.Create(anchorHtml); 
    } 

我想选择为每个主题的CSS文件。这是做到这一点的正确方法吗?

回答

1

这是正确的做法吗?

有你的代码中的许多问题,我不知道从哪里开始。如果你不关心我即将发生的争吵,你可以直接跳到我的答案的末尾,在那里我提出了你的帮助者可能的改进。

你不应该在ASP.NET MVC应用程序中硬编码URL。处理网址时,您应该始终使用网址助手。

所以不是:

path = "../../Content/themes/" + Themes.brown .ToString()+ "/style.css"; 

你应该使用的网址助手:

path = url.Content(string.Format("~/Content/themes/{0}/style.css", Themes.brown)); 

显然同样的话适用于其他的开关情况。

,然后替换:

linkBuilder.MergeAttribute("href", "@Url.Content(" + path + ")"); 

有:

linkBuilder.Attributes["href"] = path; 

而且整个switch语句很可能有一个单一的代码行代替:

var path = url.Content(string.Format("~/Content/themes/{0}/style.css", theme)); 

除你似乎没有对linkBuilder变量做任何有用的事情你构建。你建立它,并留下垃圾收集而不注入结果。

另一个问题是,您从来没有为锚点设置任何内容。您只需生成一个空的<a>。这是你想要的吗?


因此,回顾一下,你真的应该分裂成那些助手2:

一个产生的CSS <link>用于已选定的主题,一个呈现锚。

让我们把它们卷:

public static IHtmlString ThemeLink(this HtmlHelper html, Themes theme) 
{ 
    var url = new UrlHelper(html.ViewContext.RequestContext); 
    var linkBuilder = new TagBuilder("link"); 
    var path = url.Content(string.Format("~/Content/themes/{0}/style.css", theme)); 
    linkBuilder.Attributes["href"] = path; 
    linkBuilder.Attributes["rel"] = "stylesheet"; 
    linkBuilder.Attributes["type"] = "text/css"; 
    return new HtmlString(linkBuilder.ToString(TagRenderMode.SelfClosing)); 
} 

和按钮:

public static IHtmlString EditButton(
    this HtmlHelper html, 
    string action, 
    string controller, 
    bool state 
) 
{ 
    var url = new UrlHelper(html.ViewContext.RequestContext); 
    var htmlAttributes = new RouteValueDictionary(new 
    { 
     alt = "edit", 
     title = "Edit" 
    }); 
    if (state) 
    { 
     htmlAttributes["class"] = "edit_active"; 
    } 
    else 
    { 
     htmlAttributes["class"] = "edit_inactive"; 
    } 
    return html.ActionLink(" ", action, controller, null, htmlAttributes); 
} 

,然后在你的布局,或在您的<head>视图专用重写部分,您将首先包括正确的CSS:

@section Styles { 
    @Html.ThemeLink(Themes.brown) 
} 

然后在您的代码中,您将生成按钮:

@Html.EditButton("myaction", "mycontroller", true) 
@Html.EditButton("myaction", "mycontroller", false) 
... 
+0

谢谢你,对不起,我刚开始用.NET – kbaccouche 2012-03-07 08:06:17

+0

@the_ruby_racer,不用担心乱码,我们都必须从某个地方开始,然后逐步提高。 – 2012-03-07 08:07:33

+0

没有办法在同一个页面中创建两个控件,每个控件都有自己的主题? – kbaccouche 2012-03-07 08:26:15