##需求

  • Visual Studio 2015

##Note

所有的參考都需要手動加入, 所以新功課會是了解每一個參考裡面有的功能是什麼

1. Microsoft.AspNet.Diagnostics => MiddleWare to handle request(ex: welcomepage, errorpage)
2. Microsoft.AspNet.StaticFiles => 顯示靜態網頁

Microsoft.AspNet.Diagnostics Example

1
2
3
// app.useXXXXX
app.UseWelcomePage();
app.UseErrorPage();

project.json

網站所有的設定都會在這個檔案裏面做設定, 包含dependencies, webroot, exclude, frameworks, etc.

Commands

新增MVC功能

  • project.json add 「Microsoft.AspNet.Mvc」 to dependencies
  • startup.cs
1
2
3
4
5
6
7
8
9
10
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MvcOptions>(options => { });
}

public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}

如果要設定mvc route時, 在startup.cs的configure裡(in c# 6 syntax)

1
2
3
4
5
6
app.UseMvc(routes=>
{
routes.MapRoute(
name: "Default",
template: "{controller=Home}/{action=Index}/{id?}");
});

建立Configuration

  • dependence: 「Microsoft.Framework.Configuration.UserSecrets」
  • Useage:
1
2
3
4
5
6
7
8
9
10
11
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// 設定ConfiurationBuilder
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath);
configurationBuilder.AddJsonFile("config.json");
configurationBuilder.AddEnvironmentVariables();
// 必須執行Build才能建立Configuration物件
var config = configurationBuilder.Build();
// Build後
var value = config.get("key");
}

使用TagHelpers

如果想要使用MVC內建的TagHelper, 幾個需要加入的dependencies

1
2
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final" <= 在vs編輯時會將tag的部分顯示成不同的顏色及其他功能

然後在_ViewImports.cshtml加入

1
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

最近要寫一個api,其功能需要呼叫一個exe執行檔然後取得該檔回傳的dbf檔案 但是該執行檔又需要讀取網路磁碟機的檔案。這IIS就會卡住了。不管權限怎麼設定都過不去。

所以只好繞路解決了. 解法是: 建立另外一個selfhost的webapi (console mode), 在該api下執行該執行檔就可以正常運作了,因為不是透過IIS. 然後網站去呼叫那個自行運作的webapi取回結果.

雖然有點麻煩,但是至少解決問題了。(浪費我兩天的生命)

關於selft的webapi建立方式,請參考webapi selfhost

Bindingsource物件是屬於winform的,所以在web環境下的design time是沒有辦法直接設定物件到datasource的屬性裡 所以這部分需要手動加進去,作法是進入 xxx.Designer.cs的InitializeComponent()加入

1
this.bindingSource1.DataSource = typeof(Object); // replace with the object you want

這樣子回到設計模式就會出現可以設定的binding物件了

^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
********************
****讀取ini file****
********************
function getinit(mfilename,msection,mentry)
local lcinifile,lcvalue,lcbuffer,luentryvalue,lnnumbytes

*-- DECLARE DLL statements for reading/writing to private INI files
declare integer GetPrivateProfileString in Win32API ;
string cSection, string cKey, string cDefault, string @cBuffer, ;
integer nBufferSize, string cINIFile

local minivalue, mresult, mbuffersize
mbuffersize = 255
minivalue = spac(mbuffersize)
mresult=getprivateprofilestring(msection,mentry,"*NULL*",@minivalue,mbuffersize,mfilename)
minivalue=substr(minivalue,1,mresult)
if minivalue="*NULL*"
minivalue=.null.
endif
return minivalue
endfun

demo config ini file

1
2
[section]
entryName=return value

- connection retry policy
- works great with async
- four modes
    - DefaultExcutionStrategy
    - DefaultSqlExecutionStrategy
    - DbExecutionStrategy
    - SqlAzureExecutionStrategy
- throws RetryLimitExceededException

##Configuration

1
2
3
4
5
6
7
8
public class MyConfiguration : DbConfiguration 
{
public MyConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient",
() => new SqlAzureExecutionStrategy(1, TimeSpan.FromSeconds(30)));
}
}

MSDB

參考網址

取得最新的Mono http://www.mono-project.com/download/

Building Mono From a Git Source Code Checkout

To build Mono in 64 bit mode instead use:

1
2
3
4
5
6
PATH=$PREFIX/bin:$PATH
git clone https://github.com/mono/mono.git
cd mono
./autogen.sh --prefix=$PREFIX --disable-nls
make
make install

經過一段時間的實驗. 在別的framework找到可以讓backend的route支援angular html5mode了. 基本方式是如果我路由規則沒有定義的,全部指向index頁面(有ng-view的)

所以基於這個原則. mvc的路由規則就要稍微調整一下. 將原本的DefaultApp修改一下

1
2
3
4
5
6
7
8
9
10
11
12
// 舊
routes.MapRoute(
name: "DefaultApp",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// 新
routes.MapRoute(
name: "DefaultApp",
url: "app/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

然後新增

1
routes.Add(new SingleRoute());

SingleRoute Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class SingleRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = new RouteData(this, new MvcRouteHandler());
data.Values.Add("controller", "Home");
data.Values.Add("action", "Index");
return data;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}

Client angular side:

1
2
3
4
5
6
7
$stateProvider
.state('state1', {
url: "/state1",
templateUrl: "app/Home/state1" <= mvc route rule
})
$locationProvider.html5Mode(true).hashPrefix('!')

以上

HTML裡有也Element是沒有辦法focus, 原因是因為它們預設的tabindex是-1 所以要讓他們能focus, 只要改變tabindex=0. 就可以了

1
<span tabindex=0>something</span>