2015-06-28 37 views
1

我得到一個錯誤,在我看來,在ASP.net 5 Beta版6,當我使用@model或使用@inject IOptions<AppSettings> AppSettings錯誤觀點與asp.net 5測試版6

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不工作。 主頁上的鏈接不會轉換爲真正的鏈接。看到下面生成的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" 
}, 

回答

1

由於錯誤信息表明你可能丟失在你看來一個using聲明。如果您想using語句被應用到所有的意見,那麼您可以在Views文件夾下創建一個_ViewImports.cshtml文件並添加類似以下內容:

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

你的標籤助手也應該現在就開始工作。

1

截至ASP.Net 5 Beta 5MVC 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

https://stackoverflow.com/a/31281489/1706008