ASP.NET 2.0 でMetaタグを追加する方法
.NET 2.0 Beta1の時はPage.Header.Metadataを使って動的にMetaタグを追加できた。
今でもググるとそういうサンプルが多く見つかる。
ところが、Beta2の段階でこのプロパティはなくなってしまった。
http://msdn2.microsoft.com/library/system.web.ui.ipageheader.aspx
で、これからはどうすれば良いか?
下記参照。
//Get the htmlHead your aspx page is using (from the Master page)
//Master page must include the runat server attribute in the head tag:
<head runat="server">
HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;
//Create a htmlMeta object
HtmlMeta meta = new HtmlMeta();
//Specify meta attributes
meta.Attributes.Add("name", "keywords");
meta.Attributes.Add("content", "keyword1, keyword2, keyword3");
// Add the meta object to the htmlhead's control collection
head.Controls.Add(meta);
PATH以下のファイルに対する操作
path-childitemというCmdletを使う。
MSH> get-childitem C:\TEMP\ -Include *.cs -Recurse Directory: FileSystem::C:\TEMP\BLL\Properties Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 8 10 16:37 1637 AssemblyInfo.cs Directory: FileSystem::C:\TEMP\BLL Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 8 11 10:30 2044 AContext.cs -a--- 8 11 10:30 2619 CManager.cs -a--- 8 10 21:54 3580 Cryptograph.cs -a--- 8 10 14:25 531 Localization.cs -a--- 8 10 15:47 487 Logging.cs -a--- 8 10 20:24 574 Login.cs -a--- 8 10 14:41 438 PCounter.cs -a--- 8 10 15:47 916 RManager.cs -a--- 8 10 15:47 556 SConfiguration.cs -a--- 8 10 14:06 521 Usage.cs -a--- 8 10 14:34 394 UInfo.cs -a--- 8 10 14:42 402 UPreference.cs -a--- 8 10 14:31 381 Utility.cs
ちょっとこれじゃ使いにくいので、フルパスの文字列を出力するように整形する。
MSH> get-childitem C:\TEMP -Include *.cs -Recurse|foreach{combi ne-path $_.Directory $_.Name} C:\TEMP\BLL\Properties\AssemblyInfo.cs C:\TEMP\BLL\Properties\AssemblyInfo.cs C:\TEMP\BLL\ApplicationContext.cs C:\TEMP\BLL\CacheManager.cs C:\TEMP\BLL\Cryptograph.cs C:\TEMP\BLL\Localization.cs C:\TEMP\BLL\Logging.cs C:\TEMP\BLL\Login.cs C:\TEMP\BLL\PerformanceCounter.cs C:\TEMP\BLL\RoleManager.cs C:\TEMP\BLL\SystemConfiguration.cs C:\TEMP\BLL\Usage.cs C:\TEMP\BLL\UserInfo.cs C:\TEMP\BLL\UserPreference.cs C:\TEMP\BLL\Utility.cs MSH>
コマンドの実行結果が、どんなプロパティを持ったオブジェクトなのか見たい、という時は、format-listというCmdletを使うと見やすく整形してくれる。
MSH> $a=get-childitem C:\TEMP -Include *.cs -Recurse MSH> $a[0]|format-list Directory: FileSystem::C:\TEMP\BLL\Properties Name : AssemblyInfo.cs Length : 1637 CreationTime : 2005/08/10 11:34:37 LastWriteTime : 2005/08/10 16:37:08 LastAccessTime : 2005/08/11 10:30:23 VersionInfo :
で、パイプラインで必要なプロパティだけに間引きたい時には、select-objectを使う。
MSH> get-childitem C:\TEMP -Include *.cs -Recurse|select-object Name Name ---- AssemblyInfo.cs AContext.cs CManager.cs Cryptograph.cs Localization.cs Logging.cs Login.cs PCounter.cs RManager.cs SConfiguration.cs Usage.cs UInfo.cs UPreference.cs Utility.cs
Wao
http://www.leeholmes.com/blog/ADownloadManagerInMSH.aspx
I'm so glad that my tiny code inspires you to write this cool script.
コマンドの調べ方
.NET Frameworkに関して調べたい時は、MSDNのリファレンスなり、TIPSのサイトをぐぐるなり、何とでもなる。
Monad固有のCmdletはどうやって調べるかという話。
get-commandというCmdletを使うと、どんなCmdletがあるか、簡単な使い方が分かる。
MSH> get-command get-help Command Type Name Definition ------------ ---- ---------- Cmdlet get-help get-help [[-Name] String] [-Catego...
Definitionが見たいんだけど、というような時は、select-objectを使うと、パイプで渡されたレコードのうち、選択したプロパティだけを返してくれる。SQLの射影みたいな操作。
MSH> get-command get-help|select-object Definition Definition
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
または、format-listを使うと、下記のように1レコードを複数行に表示してくれる。
MSH> get-command get-help|format-list Name : get-help CommandType : Cmdlet Definition : get-help [[-Name] String] [-Category String[]] [-Component Stri ng] [-Functionality String] [-Role String] [-Verbose] [-Debug] [-ErrorAction ActionPreference] [-ErrorVariable String] [-OutVa riable String] [-OutBuffer Int32] Path : AssemblyInfo : DLL : C:\Program Files\Microsoft Command Shell\System.Management.Auto mation.DLL HelpFile : System.Management.Automation.dll-Help.xml ParameterSets : {__AllParameterSets} Type : System.Management.Automation.Commands.GetHelpCommand Verb : get Noun : help
詳しい説明が表示されるのは、get-helpというCmdlet。UNIXでいうmanみたいな感じ。
get-help [Cmdlet]というように使う。
入手方法が増えた
WinFXのベータを入れるとMSHも付いてくるそうです。
Microsoft Download Center: Windows, Office, Xbox & More
WinFXまではおなかいっぱいなので手は出さないと思いますが。
実用かどうかは
id:kkamegawaさんに気に入っていただけたようで。
今作ってる巡回プログラム、mshで書き直そうか(^^;。
私も文法を調べて喜んでるくらいなので、安定して動くかどうかは良く分からないですよ。
ループの簡単な書き方
You normally would use a counted for loop:
MSH:19 C:\temp\monad > for($x = 0; $x -lt 5; $x++) { do-something }
But here's a neat little trick to save some typing, if you don't care which iteration of the loop you're in:
MSH:19 C:\temp\monad > 1..5 | foreach { do-something }
This is a bloated and slow way to do a for loop, though, so don't use it in scripts.
forループを使うより、配列をパイプに渡してforeachを使ったほうがシンプルだし速い、という事かな?
例えば、こんな感じでIrvineなどに頼らずに自力でダウンロードするとか。
MSH:19 C:\temp\monad > $url="http://www.hoge.com/aaa/bbb/ccc/" MSH:19 C:\temp\monad > 1..50|foreach {$f=[String]::Format("{0:00}.jpg", $_); $c.DownloadFile($url + $f, $f)}
こういうのをモヒカン族って言うのじゃろか?