2014年6月24日 星期二

Code First from Database

 

Tool:Visual Studio 2013 Ultimate Update 2
OS:Windows 8.1
IIS : IIS 8
.NET Framework : 4.5.5

若沒裝Visual Studio 2013 Ultimate Update 2需下載:

從現有資料庫,產生Code First 類別

  • Step – by –Step

image

 

image

image

 

image

 

imageimage

image

image

image

將自動產生

 

image

image

image

image

image

自動產生

image

image

RUN

image

2014年6月12日 星期四

將HTML交由ASP.NET處理

 

Tool:Visual Studio 2013 Ultimate
OS:Windows 8.1
IIS : IIS 8
.NET Framework : 4.5.1
Other:ASP.NET Web Forms

若在ASP.NET Web Forms Global.asax註冊Application_BeginRequest事件

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }

    protected void Application_BeginRequest( object sender , EventArgs e ) {
        Response.Write( "Application_BeginRequest" );
    }
</script>

若有一空白的ASPX網頁如下:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

則執行ASPX時,便會自動印出以下字串,在ASPX執行之前,會先執行Application_BeginRequest事件

image

但相同網站內的HTML檔案則不會有此效果,只要設定HTML由ASP.NET處理就可,我們可以在組態檔加入以下設定

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.5.1">
      <buildProviders >
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider"   />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5.1" />
  </system.web>

  <system.webServer>
    <handlers>
      <add name="test" path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="File" preCondition="integratedMode" />
    </handlers>
  </system.webServer>

</configuration>

若有空白HTML如下

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>

</body>
</html>

也會跟ASPX一樣有相同效果

image

2014年6月6日 星期五

ASP.NET下一版懶人包

 

ASP.NET vNext and Visual Studio "14"

  • ASP.NET MVC 6 framework將MVC、Web API、Web Pages整合成一個framework
  • 你需要使用 Visual Studio "14" 來建立與編譯 MVC 6 專案
  • MVC 6 專案可以在命令提示字元編譯並執行(不需VisualStudio)
  • 專案檔案(原*.csproj)改用JSON格式存放 - project.json
  • 新增startup class,在Configure方法設定HTTP request pipeline ,或設定MVC 6路由
  • ASP.NET Web API Controller改繼承自Controller (前一版繼承自ApiController)類別
  • route template 新增{area} parameter,以定義Areas(前一版叫用AreaRegistration.RegisterAllAreas方法)
  • Web API 將支援Area (前一版不支援,新版MVC與Web API整合成一個Framework,可以共用路由、filter、model binding...等等功能)
  • 新增ViewComponent,讓View可以使用Html.Component呼叫Action,並顯示結果
  • 新增POCO Controllers,不需繼承自 Microsoft.AspNet.Mvc.Controller

 

參考資源

總網頁瀏覽量