Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

error CS1061 C# '' does not contain a definition for '' and no extension method '' accepting a first argument of type '' could be found

$
0
0

Hi,

Very new to ASP.NET so I have been following the MVC MVA course on ASP.NET5 (most excellent so far I have to say but have come across a compile issue with the code as used on the course. I understand that the Configuration mechanism has been updated in ASP.NET5 so I have followed what I think is correct in trying to add the MyOptions collection to the Services collection so it can be accessed in the Controller. The code is below. Unfortunately I get the following error message fro the line in the Controller "var colour = o.Options.colour;". Can anyone advise me where I have gone wrong please?

The error message is: CS1061 C# '' does not contain a definition for '' and no extension method '' accepting a first argument of type '' could be found

My code is as follows:
startup.cs:
===========
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace App_Endorsement
{
public class MyOptions
{
public string colour { get; set; }
public string key { get; set; }
public string key2 { get; set; }
public string welcomestring { get; set; }
}

public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddJsonFile("config.json") // added by me which allows you to pull in specific variables as key/value pairs:
.AddJsonFile($"config.{env.EnvironmentName}.json", true) // environment-specific variables
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfiguration Configuration { get; set; }

// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
var key = Configuration["key"];
var key2 = Configuration["key2"];
var colour = Configuration["colour"];

// This adds an “accessor” to the Services collection that allows you to collect the MyOptions
// collection which now allows you to access the options anywhere in the App:
services.Configure<MyOptions>(Configuration);


controller:
===========
using App_Endorsement.Models;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.OptionsModel;

namespace App_Endorsement.Controllers
{
public class EndorsementController : Controller
{
// This adds an “accessor” to the Services collection that allows you to collect the
// MyOptions collection which now allows you to access the options anywhere in the App.
// This can then be passed into a View or whatever to be used….
public EndorsementController(IOptions<MyOptions> o)
{
var colour = o.Options.colour;
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>