2012年11月29日 星期四

MVC 4 應用程式呼叫WCF

想使用Visual Studio 2012寫個MVC4呼叫WCF的範例,居然出現一個問題。Add Service Reference功能居然不會產生Proxy….Orz…

New – ASP.NET MVC 4 Web Application

image

image

加入一個WCF Service:

image

image

MVC專案-Add Service Reference

image

Discover:

image

oh~YA~看一下工具產生出來的Reference.cs檔案,裏頭居然沒有Proxy程式碼:空空如也~ Config檔案也沒有任何和WCF服務相關的組態設定….ORZ…本來還猜是不是runtime才會產生,結果也不像。

image

只好手動產生proxy 程式碼:執行svcutil 產生proxy與config

image

將output config中的<system.serviceModel>區段複製到

image

MVC專案web.config檔:

image

將產生的service.cs程式加入 MVC專案之中

image

Add Controller

image

image

建立proxy呼叫服務,將執行結果放到ViewBag.result

image

產生view

image

image

印出執行結果

image

按F5執行,可以看到執行結果

image

2012年11月8日 星期四

Q&A: EntityDataSource

 

  • VS2012+ Windows 8
  • ASP.NET 4.5
  • for Lab1.5

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

CTRL+F5

image

image

 

image

CTRL+F5

image

2012年11月7日 星期三

Q&A ASP.NET Dynamic Data 實體網站 -客製化

 

延續此文範例 Q&A ASP.NET Dynamic Data 實體網站

  • VS2012+ Windows 8
  • ASP.NET 4.5
  • for Lab2.3 自訂網頁範本

修改global.asax

image

加一個authors目錄

image

image

image

客製化,如加上字串

image

run

image

Q&A ASP.NET Dynamic Data 實體網站

 

  • VS2012+ Windows 8
  • ASP.NET 4.5
  • for Lab2.1 使用動態資料與語意標註

Visual Studio 2012 的ASP.NET Dynamic Data 實體網站範本,使用Entity Framework 5.0,因此要啟用Dynamic Data的語法有一點不同:

1.

image

Add ADO.NET Entity Data Model

image

image

image

image

image

image

 

修改global.asax,RegisterRoutes方法:

PS:這部分和VS2010最大不同的是DefaultModel.RegisterContext的寫法,需這樣寫

DefaultModel.RegisterContext(() =>
{
  return ((System.Data.Entity.Infrastructure.IObjectContextAdapter)new pubsEntities()).ObjectContext;
}, new ContextConfiguration() { ScaffoldAllTables = true });

ex:

image

執行網站測試

image

App_Code目錄,Add authors class:

[MetadataType(typeof(AuthorsMetadata))]
[DisplayName("作者")]
public partial class authors
{
  private class AuthorsMetadata
  {
    [DisplayName("姓氏")]
    public string au_lname { get; set; }
    [DisplayName("名字")]
    public string au_fname { get; set; }
  }
}

Run

image
image

總網頁瀏覽量