2014年10月27日

[IIS]在Azure WebSite服務上透過URL Rewrite Module將前端HTML的要求轉給ASPX處理

有兩種方式

1. 透過URL Rewrite的Rewrite轉址

在的web.config中加入以下這一段設定

<configuration>

<!--- Ommitted à

<system.webServer>

        <rewrite>

            <rules>

                <rule name="TEST" stopProcessing="true">

                    <match url="[^?#]*\w+\.(?=html|php)" />

                    <action type="Rewrite" url="{R:0}aspx" logRewrittenUrl="true" />

                </rule>

            </rules>

        </rewrite>

    </system.webServer>

</configuration>

其中,html | php這邊可以加入任何需要轉址的附檔名;設定後,當使用者存取abc.html時,網址列仍然會顯示abc.html,但實際上是由abc.aspx來處理

 

2. 透過URL Rewrite的Redirect轉址

在web.config中加入以下這一段設定

<configuration>

<!--- Ommitted à

<system.webServer>

        <rewrite>

            <rules>

                <rule name="TEST" stopProcessing="true">

                    <match url="[^?#]*\w+\.(?=html|php)" />

                    <action type="Redirect" url="{R:0}aspx" logRewrittenUrl="true"  redirectType="Permanent"/>

                </rule>

            </rules>

        </rewrite>

    </system.webServer>

</configuration>

其中,html | php這邊可以加入任何需要轉址的附檔名;設定後,當使用者存取abc.html時,Server會告訴瀏覽器真正的網址是abc.aspx,然後使用者會被轉到abc.aspx;網址列會顯示abc.aspx。

2014年10月23日

在IIS上設定HTTP轉址為HTTPS

· 開啟IIS Manager

· 在需要做HTTP轉HTTPS的網站上開啟URL Rewrite
clip_image001

· 新增規則

clip_image003

· 建立一個空的規則

clip_image005

· 設定Inbound Rule

clip_image007

· 展開Condition並新增一個規則

clip_image009

· 設定 Condition如下

clip_image010

· 新增一個Action

clip_image012

· 完成後按下Apply

clip_image013

·

2014年10月1日

[Azure]DocumentDB開發

剛剛建了一個DocumentDB來測試看看,下面是一些測試過程的sample code與心得。

DocumentDB的架構如下:

image

首先是建立資料庫與Collection,並將文件加入Collection

    private async void CreateDB()
{
//宣告三個物件
dynamic obj1 = new {
Id = "1",
Name ="Demo1"
};
dynamic obj2 = new {
Id = "2",
Name = "Demo2"
};
dynamic obj3 = new
{
Id = "3",
Name = "Demo3",
DisplayName = "Test display" //測試不同物件型態
};
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var database = new Database
{
Id = "DemoDB"
};
database = await client.CreateDatabaseAsync(database);

var collection = new DocumentCollection { Id = "Demo" };
collection = await client.CreateDocumentCollectionAsync(database.SelfLink, collection);

await client.CreateDocumentAsync(collection.SelfLink, obj1);
await client.CreateDocumentAsync(collection.SelfLink, obj2);
await client.CreateDocumentAsync(collection.SelfLink, obj3);//不同物件型態一樣可以新增成功
}
}



 



接著,查詢剛剛加入的文件



    protected async void Button2_Click(object sender, EventArgs e)
{
using (var client = new DocumentClient(new Uri("https://<<yourdb>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();

var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();

var query = client.CreateDocumentQuery(collection.DocumentsLink, "select * from Demo d where d.DisplayName = 'Test display'");
var obj = query.AsEnumerable().ToArray();


await client.DeleteDatabaseAsync(database.SelfLink);
}
}



 



 



測試Stored Procedure



    protected async void Button3_Click(object sender, EventArgs e)
{
StoredProcedure sp = new StoredProcedure()
{
Id = "testSP",
Body = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,"sp.txt"))
};
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();
var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();
var sps = client.CreateStoredProcedureQuery(collection.StoredProceduresLink);
if (sps.AsEnumerable().Count() > 0)
{
await client.DeleteStoredProcedureAsync(sps.AsEnumerable().Single().SelfLink);
}
sp = await client.CreateStoredProcedureAsync(collection.SelfLink, sp);
}
}



 



Stored procedure定義如下:



function getTestItem(id1, id2){
var cxt = getContext();
var colleciton = cxt.getCollection();
var response = cxt.getResponse();
colleciton.queryDocuments(colleciton.getSelfLink(),
'Select * from Demo d where d.Id="' + id1 + '" or d.Id="' + id2 + '"'
,function(err, documents){
if(err){
response.setBody(err);
}else{
if(documents.length > 0){
response.setBody(response.getBody() + JSON.stringify(documents));
}
}
}
);

}



 



呼叫此Stored Procedure,檢查result物件可以看到回傳的JSon物件結構。



    protected async void Button4_Click(object sender, EventArgs e)
{
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();
var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();

var sp = client.CreateStoredProcedureQuery(collection.StoredProceduresLink).AsEnumerable().Where(s => s.Id == "testSP").SingleOrDefault();

dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>(sp.SelfLink, "3", "1");

}
}



 



 



 




Blog Archive

About Me