0

Dot NET CORE I am trying to add MVC view PAGE in MVC CORE web API solution. I have added the controller and view but as soon as I try to access it, it gives run time error as below.

While creating the project if I select Web Application everything works fine .But I have an existing rest API Project and this makes me create two project. I think we can extend the same project to host Rest API as well as web page if needed. Cant we ?

I am continuously getting this run time error

Startup.cs

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(LogLevel.Debug);
    app.UseDeveloperExceptionPage();
    app.UseCors(options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
    app.UseStaticFiles();
    app.UseOAuthValidation();
    app.UseOpenIddict();
    app.UseMvcWithDefaultRoute();
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
    services.AddMvc();
    services.AddAutoMapper();
    services.AddDbContext<ApplicationDbContext>(options =>
    
    services.AddIdentity<ApplicationUser, IdentityRole<Guid>>()
        .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
        .AddDefaultTokenProviders();
    services.AddOpenIddict<ApplicationUser, IdentityRole<Guid>, ApplicationDbContext, Guid>()
        
        .AllowAuthorizationCodeFlow()
        .AllowPasswordFlow()
        .AllowRefreshTokenFlow()
        .DisableHttpsRequirement()

        .AddEphemeralSigningKey();
    services.AddTransient<IEmailSender, AuthMessageSender>();
}

Project.json

{
  "buildOptions": {
    "emitEntryPoint": true,
    "debugType": "portable"
  },
  "dependencies": {

    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "OpenIddict": "1.0.0-alpha2-0448",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
    "AspNet.Security.OAuth.Validation": "1.0.0-alpha2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.1",
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.AspNetCore.Cors": "1.0.0",
    "Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.0.0",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
    "AutoMapper": "5.1.1",
    "AutoMapper.Extensions.Microsoft.DependencyInjection": "1.1.2",
    "Microsoft.DotNet.ProjectModel": "1.0.0-rc3-003121",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "datalayer": "1.0.0.0",
    "Common": "1.0.0-*"
  },
  "frameworks": {
    "netcoreapp1.0": { }
  },
  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview2-final"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final"
    }
  },
  "scripts": {
    "prepublish": [ "bower install" ],
    "postpublish": "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
  },
  "publishOptions": {
    "include": [ "wwwroot" ],
    "includeFiles": [ "appsettings.json" ]
  }
}

Controller

public class ActivationController : Controller
{
    // GET: /<controller>/
    public IActionResult Index()
    {
        return View();
    }
}

View (Index.cshtml)

<h1>Hello world!</h1>
7
  • What's the code in the cshtml file? Commented Nov 11, 2016 at 5:40
  • <h1>Hello world!</h1> thats all Commented Nov 11, 2016 at 6:18
  • Please, update your answer with your project.json, Startup.cs, your MVC Controller and your View. Commented Nov 11, 2016 at 10:43
  • Updated in the question. Please have a look Commented Nov 11, 2016 at 11:47
  • Are you using a other repositories besides Nuget for your packages? I could not find the "OpenIddict" package, for example. Commented Nov 11, 2016 at 12:16

1 Answer 1

1

It seems we need to set preserveCompilationContext to true in project.json . It's necessary for runtime compilation of Razor views .

  "buildOptions": {
        "emitEntryPoint": true,
        "debugType": "portable",
        "preserveCompilationContext": true
      },
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.