1. 程式人生 > >VS2010升級VS2012必備(MVC4 WebPage2.0 Razor2.0資料彙集)

VS2010升級VS2012必備(MVC4 WebPage2.0 Razor2.0資料彙集)

剛把專案升級到2012,發現發生了很多變化,以下是最近看過的網站和資料彙集,供需要者參考。

本文在最近一個月可能會不斷更新。

Razor2.0

新特性介紹:取消了@Href和@Url.Content;可以使用conditional attribute了(就是class = ""等將不會顯示)空單元(void Eliment);新的syntax tree;

WebPage2.0

新特性介紹:包含詳細的如何通過Google/Yahoo/Faceboock使用者登入站點(OAuth和OpenID支援)

MVC3到MVC4升級中遇到的問題彙總

登入機制換了,和使用者相關的操作會出現一些錯誤

現在是SimpleMembership了,也就是每個使用者有1個ID,一個使用者名稱是必備的,其他的都可以自定義(系統幫助定義了一些)。注意MVC4會生成一個新的aspnet.db,結構於原來的不同。web.config中需要做相應的修改。其他大致可能引發的問題包括:1. 所有使用Membership和其他早期身份認證操作的地方都會有問題
但它不會說是Membership的問題,而是說“aspnetdb檔案找不到”或“xxlog.mdf與Xx不匹配”之類的。現在要獲取所有使用者,請用:
var users = new UsersContext().UserProfiles.ToList();
刪除則是:
                userContext.UserProfiles.Remove(user);
                userContext.SaveChanges();
這個UsersContext(在新建的MVC4專案中,它會自動出現在一個叫做AccountModels.cs的檔案中;如果是升級的專案,可以新建一個MVC4專案然後拷貝過來)。據稱他除了能在本地資料庫中儲存使用者資訊外,還可可以接受OpenID等外網的使用者。有點像“用QQ號登陸”。2. 所有[Authorize[Roles="XXX"]的地方會出錯。
比如這個:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)


這是因為他還在嘗試到舊的aspnetdb中找Role。這時候,請到一個~\Filters\InitializeSimpleMembershipAttribute.cs檔案中,找這句話:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

並把它挪到(此語句只能呼叫一次,故需要挪動,或註釋掉):~\Global.asax.cs中儘早呼叫,比如:
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            AuthConfig.RegisterAuth();
            ...

            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        }
另外在一個叫做 FilterConfig.cs檔案中,請加上第二句話:
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new InitializeSimpleMembershipAttribute());
        }
    }
原文見:http://stackoverflow.com/questions/12342398/simplemembership-mvc4-authorizeattribute-and-roles

Razor的一些小改動

site.css大規模改動如果發現網站的文字、表格變化了,是因為預設的site.css變了。比如現在表格都變成沒有格子線條的了。
@this.XXX不支援錯誤:@this.InitLayout(xxx) //自己寫的一個Helper,現在出錯。正確:@(this.InitLayout(xxx) )Layout方案的變化現在所有的頁面都預設使用~/_ViewStart.cshtml(無需呼叫,自動執行),其中只有一句話:
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
_Layout自動其提供的整體佈局、色調、圖片,很多在site.css中(比如有兩處使用了background,整個頁面上邊黑色邊緣則是border: 10px #000形成的)。如果想沿用原來自己的Layout,可以刪除這句話,就會繼續呼叫原來的Layout了。如果是新建的MVC4專案,可通過修改_Layout.cshtml檔案形成自己的Layout。某些頁面或頁面的區域性突然不顯示就是上面的Layout機制變化造成的。如果沒有刪除上面~/_ViewStart.cshtml中
Layout = "~/Views/Shared/_Layout.cshtml";
這句話,在任何其他地方設定Layout可能會導致錯誤,但不報錯!只是從此處以後的內容不再顯示。比如如果在你自己的View再寫:
Layout = "~/Views/Shared/_Layout.cshtml";
就會出錯。這似乎表明,Layout只能設定一次了,第二次會出錯。。所以,要麼刪除_ViewStart那次,要麼自己不要再設定了。我選擇了前者,因為我們做了一些複雜的設定工作。asp.net的本意是說可以在_ViewStart.cshtml中進行程式設計,確認到底用哪個Layout。

LocalDB資料庫升級

SQL Server Express2008的資料庫可能不支援了有兩種出路,一種升級到SSCE4.0(需要資料遷移,限制4G),一種升級到SSE2012(VS可以幫你自動升級,限制10G)。升級過程如下(在VS2012裡邊完成的):
  1. In Server Explorer, choose the Connect to Database button.

  2. In the Add Connection dialog box, specify the following information:

    • Data Source: Microsoft SQL Server (SqlClient)

    • Server Name: (LocalDB)\v11.0

    • Attach a database file: Path, where Path is the physical path of the primary .mdf file.

    • Logical Name: Name, where Name is the name that you want to use with the file.

  3. Choose the OK button.

  4. When prompted, choose the Yes button to upgrade the file.

如果不行,請參考原文:http://msdn.microsoft.com/en-us/library/hh873188.aspx我選了後者,因為沒找到資料遷移的自動工具,如果有,歡迎回復,謝謝:)