標籤

ASP.NET MVC (29) Visual C# (15) JQuery (10) Plugins (8) JQuery Plugins (6) JavaScript (6) MySQL (5) CSS (4) LinQ (4) Mac OS (4) CentOS (3) Design Pattern (3) Entity Framework (3) IIS (3) Python (3) Windows (3) php (3) Docker (2) LAMP (2) SQL Server (2) WCF (2) .NET (1) .NET Core (1) AWS (1) Browser (1) GIS (1) IE (1) Internet Security (1) Linux (1) Platform (1) React (1) SEO (1) Testing (1) VMware (1) Windows 7 (1) cookie (1) curl (1) laravel (1) phpBB (1) session (1) 中古屋 (1) 透天 (1) 閒言閒語 (1) 面試 (1) 鳥松 (1)

2011年6月7日 星期二

[ASP.NET MVC] 如何在主版頁面(Masterpage)中加入固定標題(Tilte)-續

之前寫了一篇:http://maidot.blogspot.com/2011/03/aspnet-mvc-masterpagetilte.html


如果我們希望Title是動態產生的該怎麼辦?其實很簡單,只需要:
< title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>
< /title>


沒錯,就是這樣簡單明瞭,其實如果使用Razor View是不會有任何問題的。

Reference:http://stackoverflow.com/questions/6291131/how-to-set-the-global-text-of-title-in-site-master-page-using-asp-net-mvc-2-3


====[以下為舊資料,請勿參考]====
現在發現如果要這樣搞:
   < title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
< asp:Literal ID="ltlTitleBack" runat="server" Text='<%: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
< /title>

是行不通的,它會顯示< asp:Literal不能包含子控制項,不過我找到一個可行的解決方案,但不知道是不是最好的:http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

需要將Site.Master的程式碼改為:
   < title>
< asp:ContentPlaceHolder ID="TitleContent" runat="server" />
< asp:Literal ID="ltlTitleBack" runat="server" Text='<%$ Code: System.Web.Configuration.WebConfigurationManager.AppSettings["SiteTile"] %>' >
< /title>


然後某個Class中加入
using System.Web.Compilation;
using System.CodeDom;
using System.Web.UI;
//修正Literal
[System.Web.Compilation.ExpressionPrefix("Code")]
public class CodeExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
object parsedData, ExpressionBuilderContext context)
{
return new CodeSnippetExpression(entry.Expression);
}
}


最後在Web.config中的區段中加入下列程式碼:
     

2011年6月3日 星期五

[ASP.NET MVC] 在View中取得目前的Controller, Action 的名稱

MVC3使用方式:
ViewContext.Controller.ValueProvider.GetValue("action").RawValue

ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("id").RawValue

MVC2:
ViewContext.Controller.ValueProvider["action"].RawValue
ViewContext.Controller.ValueProvider["controller"].RawValue

ViewContext.Controller.ValueProvider["id"].RawValue

Reference:

ASP.NET MVC - Current Action