asp.net 5 beta 6 视图错误

Error in view with asp.net 5 beta 6

我在 ASP.net 5 Beta 6 中使用 @model@inject IOptions<AppSettings> AppSettings

时出现错误

DI 似乎是问题所在。

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

/Views/Account/Login.cshtml

The type or namespace name 'LoginViewModel' could not be found (are you missing a using directive or an assembly reference?)
@model LoginViewModel

而且 taghelpers 也没有工作。 主页上的 link 不会转换为真正的 link。请参阅下面生成的 html。

<a id="registerLink" asp-controller="Account" asp-action="Register">Register</a>

我认为它与软件包或启动代码有关。请参阅下面的代码。

using DBC.Models.DB;
using DBC.Services;
using Microsoft.AspNet.Authentication.Facebook;
using Microsoft.AspNet.Authentication.Google;
using Microsoft.AspNet.Authentication.MicrosoftAccount;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics;
using Microsoft.AspNet.Diagnostics.Entity;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Configuration.EnvironmentVariables;
using Microsoft.Framework.Configuration.UserSecrets;
using Microsoft.Data.Entity.SqlServer;
using Microsoft.Framework.Logging;

namespace DBC
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Setup configuration sources.
            var configuration = new ConfigurationBuilder(System.IO.Path.GetFullPath(System.IO.Path.Combine(env.WebRootPath, "..")))
                .AddJsonFile("config.json")
                .AddJsonFile($"config.{env.EnvironmentName}.json", true);

            if (env.IsEnvironment("Development"))
            {
                // This reads the configuration keys from the secret store.
                // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            }
            configuration.AddUserSecrets();
            configuration.AddEnvironmentVariables();
            Configuration = configuration.Build();
        }

        public IConfiguration Configuration { get; set; }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add Application settings to the services container.
            services.Configure<AppSettings>(Configuration.GetConfigurationSection("AppSettings"));

            // Add EF services to the services container.
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(
                option => option.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
                );

            // Add Identity services to the services container.
            services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 4;
                options.Password.RequireLowercase = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireNonLetterOrDigit = false;
                options.SignIn.RequireConfirmedEmail = true;
                options.User.RequireUniqueEmail = true;
            })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            // Configure the options for the authentication middleware.
            // You can add options for Google, Twitter and other middleware as shown below.
            // For more information see http://go.microsoft.com/fwlink/?LinkID=532715
            services.Configure<FacebookAuthenticationOptions>(options =>
            {
                options.AppId = Configuration["Authentication:Facebook:AppId"];
                options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            });

            services.Configure<MicrosoftAccountAuthenticationOptions>(options =>
            {
                options.ClientId = Configuration["Authentication:MicrosoftAccount:ClientId"];
                options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"];
            });
            services.Configure<GoogleAuthenticationOptions>(options =>
            {
                options.ClientId = Configuration["Authentication:Google:ClientId"];
                options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                options.Caption = "googleplus";
            });
            //services.Configure<OAuthAuthenticationOptions>(options =>
            //{
            //    options.ClientId = Configuration["Authentication:Google:ClientId"];
            //    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
            //});

            // Add MVC services to the services container.
            services.AddMvc();
            //Own DBC service
            services.AddSingleton<MessageServices, MessageServices>();
            services.AddTransient<IEmailTemplate, EmailTemplate>();
            IConfiguration config = Configuration.GetConfigurationSection("mailSettings");
            services.Configure<MessageServicesOptions>(config);

            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
            // services.AddWebApiConventions();
        }

        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
        {
            // Configure the HTTP request pipeline.

            // Add the console logger.

            loggerfactory.AddConsole(minLevel: LogLevel.Warning);

            // Add the following to the request pipeline only in development environment.
            if (env.IsEnvironment("Development"))
            {
                app.UseErrorPage();
                //app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll);
            }
            else
            {
                // Add Error handling middleware which catches all application specific errors and
                // sends the request to the following path or controller action.
                app.UseErrorHandler("/Home/Error");
            }
            Configure2(app);
        }

        public void Configure2(IApplicationBuilder app)
        {
            // Add static files to the request pipeline.
            app.UseStaticFiles();

            // Add cookie-based authentication to the request pipeline.
            app.UseIdentity();

            // Add authentication middleware to the request pipeline. You can configure options such as Id and Secret in the ConfigureServices method.
            // For more information see http://go.microsoft.com/fwlink/?LinkID=532715
            // app.UseFacebookAuthentication();
            app.UseGoogleAuthentication();
            // app.UseMicrosoftAccountAuthentication();
            // app.UseTwitterAuthentication();

            // Add MVC to the request pipeline.
            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller}/{action}/{id?}", new { controller = "Home", action = "Index" });

                // Uncomment the following line to add a route for porting Web API 2 controllers.
                //routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
            });
            //app.UseWelcomePage();
        }
    }
}

还有 project.json

"dependencies": {
    "EntityFramework.Commands": "7.0.0-beta6-13586",
    "EntityFramework.SqlServer": "7.0.0-beta6-13586",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta6-12600",
    "Microsoft.AspNet.Authentication.Facebook": "1.0.0-beta6-12600",
    "Microsoft.AspNet.Authentication.Google": "1.0.0-beta6-12600",
    "Microsoft.AspNet.Authentication.MicrosoftAccount": "1.0.0-beta6-12480",
    "Microsoft.AspNet.Authentication.Twitter": "1.0.0-beta6-12600",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta6-13321",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta6-13169",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6-12644",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta6-11976",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta6-12361",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta6-12110",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-beta6-13550",
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta6-12409",
    "Microsoft.Framework.Configuration": "1.0.0-beta6-11520",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta6-11520",
    "Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta6-10457",
    "Microsoft.Framework.Logging": "1.0.0-beta6-11516",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta6-11456",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta4",
    "npm": "1.4.15.2",
    "Microsoft.AspNet.MVC": "6.0.0-beta6-14192",
    "Microsoft.AspNet.Mvc.Extensions": "6.0.0-beta6-14192",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta6-14192"
},

由于错误消息表明您可能在视图中缺少 using 语句。如果您希望 using 语句应用于所有视图,那么您可以在 Views 文件夹下创建一个 _ViewImports.cshtml 文件并添加如下内容:

@using MvcSample.Web.Models
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

您的标签助手现在也应该开始工作了。

ASP.Net 5 Beta 5 开始,MVC 6 项目 有几个重大变化;最初,要在视图中使用的引用包含在名为:

的文件中

_GlobalImport.cshtml

现在已重命名为:

_ViewImports.cshtml

官方公告:https://github.com/aspnet/Announcements/issues/27

Visual Studio 2015 RC 模板尚未更新以反映这些(和其他)重大更改。 ASP.Net 团队声明 ASP.Net 5 的最终版本将在 Visual Studio 2015 发布后以 NuGet 包的形式发布。

有关 beta4(随 Visual Studio 2015 RC 一起提供)和 beta5 之间变化的更深入的答案,关于升级说明,我推荐以下答案,这也与 beta6: