`
923723914
  • 浏览: 634419 次
文章分类
社区版块
存档分类
最新评论

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


深入学习新的Authority机制:
2. 这篇文章说了如何使用Role(感觉不太好,不应该把这些安全内容放到View里边,应该放到Controller里边。但至少知道怎么弄了):http://www.asp.net/web-pages/tutorials/security/16-adding-security-and-membership

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. InServer Explorer, choose theConnect to Databasebutton.

  2. In theAdd Connectiondialog box, specify the following information:

    • Data Source:Microsoft SQL Server (SqlClient)

    • Server Name:(LocalDB)\v11.0

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

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

  3. Choose theOKbutton.

  4. When prompted, choose theYesbutton to upgrade the file.

如果不行,请参考原文:http://msdn.microsoft.com/en-us/library/hh873188.aspx

我选了后者,因为没找到数据迁移的自动工具,如果有,欢迎回复,谢谢:)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics