Thom Kiesewetter
Thom Kiesewetter

Reputation: 7324

Error in view with asp.net 5 beta 6

I get an error in my view In ASP.net 5 Beta 6 when I use @model or use @inject IOptions<AppSettings> AppSettings

DI seems the problem.

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

And also the taghelpers are not working. The link on the homepage doesn't transform to a real link. See the generated html below.

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

I think it is something with the packages or startup code. See the code below.

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();
        }
    }
}

And the 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"
},

Upvotes: 2

Views: 1389

Answers (2)

dmcquiggin
dmcquiggin

Reputation: 3379

As of ASP.Net 5 Beta 5, the MVC 6 projects have several breaking changes; originally, references to be used in Views were include in a file named:

_GlobalImport.cshtml

This has now been renamed to:

_ViewImports.cshtml

The official announcement: https://github.com/aspnet/Announcements/issues/27

Visual Studio 2015 RC templates have not yet been updated to reflect these (and other) breaking changes. The ASP.Net team have state that the final release of ASP.Net 5 will occur after the release of Visual Studio 2015, in the form of NuGet packages.

For a more in depth answer regarding changes between beta4 (as shipped with Visual Studio 2015 RC) and beta5, with instructions on upgrading, I recommend the following answer, which is also relevant to beta6:

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

Upvotes: 2

Kiran
Kiran

Reputation: 57969

As the error message indicates you are probably missing a using statement in your view. If you would like the using statements be applied to all views, then you can create a _ViewImports.cshtml file under your Views folder and add something like following:

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

Your tag helpers also should start working now.

Upvotes: 2

Related Questions