Missing extension methods in HtmlHelper using NHaml - c#

I discovered NHaml some days ago and it's a great project.
When I try to use MVC2 Html helpers like Html.LabelFor(), Html.TextBoxFor(); the views won't compile.
Example:
error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'LabelFor' and no extension method 'LabelFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0185: textWriter.Write(" ");
0185: textWriter.Write(Convert.ToString(Html.LabelFor(model => model.Username)));
0187: textWriter.WriteLine();
error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'TextBoxFor' and no extension method 'TextBoxFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0194: textWriter.Write(" ");
0194: textWriter.Write(Convert.ToString(Html.TextBoxFor(model => model.Username)));
0196: textWriter.WriteLine();
I tried to add assemblies and namespaces in the nhaml's Web.config section but it doesn't change anything.
I'm using :
System.Web.Mvc 2.0
.NET Framework 3.5 SP1
Nhaml 1.5.0.2 from git trunk (and tried other builds)
My NHaml configuration is:
<nhaml autoRecompile="true" templateCompiler="CSharp3" encodeHtml="false" useTabs="false" indentSize="2">

It looks like you have an assembly reference problem.
You are probably referencing the MVC 1.0 assemblies, instead of 2.0 assemblies?

The problem is the view class contains a non-generic HtmlHelper. Or some new extension methods requires the ViewData.Model's type.
To correct this problem, change the property and instantiation in NHaml.Web.Mvc/NHamlMvcView.cs.
//public HtmlHelper Html { get; protected set; } // line 42
public HtmlHelper<TModel> Html { get; protected set; }
//Html = new HtmlHelper( viewContext, this ); // line 37
Html = new HtmlHelper<TModel>( viewContext, this );
Rebuild and use :)

As far as I can see the new MVC helpers are not supported, actually only a limited amount of HtmlHelpers are namely LinkExtensions. As a wild guess, you can possibly try to adding the LabelExtensions to the setup of the NHaml viewengine in the NHaml.Web.Mvc/NHamlMvcViewEngine.cs file (since you do have the source) and check if that works.
private void InitializeTemplateEngine()
{
// snip
_templateEngine.Options.AddReference( typeof( LabelExtensions ).Assembly.Location ); // Line 50
}

Related

'IServiceCollection' does not contain a definition for 'AddIdentity'

Before I tested this line on a web application in core 5.
services.AddIdentity<Operator, IdentityRole>().AddEntityFrameworkStores<StorageContext>().AddDefaultTokenProviders();
This works fine in startup class.
Now I want to know how to implement it in windows form, Core 7. Because I just get this error -
'IServiceCollection' does not contain a definition for 'AddIdentity'
and no accessible extension method 'AddIdentity' accepting a first
argument of type 'IServiceCollection' could be found (are you missing
a using directive or an assembly reference?)
Am I missing assemblies? what are they?
this is my code now -
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddScoped<IStorageRepository, StorageRepository>();
services.AddDbContext<StorageContext>(option =>
{
option.EnableSensitiveDataLogging(true);
option.UseSqlServer(configuration["Data:Storage:ConnectionString"]);
});
services.AddIdentity<Operator, IdentityRole>().AddEntityFrameworkStores<StorageContext>().AddDefaultTokenProviders();
});
}
Does this AddIdentity class works in Winforms?
Just try to install Microsoft.AspNetCore.Identity package.
I had the same problem, just try to add this nugget package Microsoft.AspNetCore.Identity.UI. I add it and it start see it

What is the dotnet 6 equivalent of localizer.WithCulture?

When my project was using
<TargetFramework>netcoreapp3.1</TargetFramework>
I could get the localization injected in my page constructor:
IStringLocalizer<Strings> _localizer;
public IndexModel(IStringLocalizer<Strings> localizer) {
_localizer = localizer;
}
but still retrieve an alternative language's collection of strings:
var localizer = _localizer.WithCulture(new CultureInfo(cultureCode));
var translatedStr = localizer[c.Value].ToString();
Being in the process of upgrading to:
<TargetFramework>net6.0</TargetFramework>
The _localizer.WithCulture code shows this error in VS Code:
'IStringLocalizer<Strings>' does not contain a definition for 'WithCulture' and no accessible extension method 'WithCulture' accepting a first argument of type 'IStringLocalizer<Strings>' could be found (are you missing a using directive or an assembly reference?)
What is the dotnet 6 version of this?
If I switch back to netcoreapp3.1 I can clearly see the warning:
'IStringLocalizer.WithCulture(CultureInfo)' is obsolete: 'This method is obsolete. Use `CurrentCulture` and `CurrentUICulture` instead.'
But what are the CurrentCulture and CurrentUICulture it is referring to?

Method overload of ConnectReceiveEndpoint changed after upgrading MassTransit

I am upgrading from 5.3.2 to version 5.5.3 and the following code does not compile. I get the errors
'IReceiveEndpointConfigurator' does not contain a definition for 'BindMessageExchanges' and no accessible extension method 'BindMessageExchanges' accepting a first argument of type 'IReceiveEndpointConfigurator' could be found (are you missing a using directive or an assembly reference?)
'IReceiveEndpointConfigurator' does not contain a definition for 'Bind' and no accessible extension method 'Bind' accepting a first argument of type 'IReceiveEndpointConfigurator' could be found (are you missing a using directive or an assembly reference?)
It seems that p is IReceiveEndpointConfigurator and not IRabbitMqReceiveEndpointConfigurator anymore. There seems to be changes on the overloaded methods or extension methods.
What overloaded method should I use instead? The code is used in testscenarios to hook up temporary queues/exchanges. _host is of type IRabbitMqHost
_handle = _host.ConnectReceiveEndpoint(p =>
{
p.BindMessageExchanges = false;
p.Handler<T>(context =>
{
_receivedMessage = context.Message;
_messageReceived.Set();
return Task.CompletedTask;
});
p.Bind<T>(z =>
{
z.ExchangeType = "direct";
z.RoutingKey = _routingKey;
});
});
The method is right there:
https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit.RabbitMqTransport/IRabbitMqHost.cs#L47
Are you specifying a queue name?

ASP.NET Identity Is default ApplicationUserManager implementing IUserEmailStore?

Hello,
I was looking into ASP.NET Identity source, particulary, in UserValidator. I wanted to rewrite it for my own project, to be able to change error messages. So I copy that class into my project. When I try to use it - I get an error - 'Microsoft.AspNet.Identity.UserManager<TUser,TKey>' does not contain a definition for 'GetEmailStore' and no extension method 'GetEmailStore' accepting a first argument of type 'Microsoft.AspNet.Identity.UserManager<TUser,TKey>' could be found (are you missing a using directive or an assembly reference?)
at this line
var email = await Manager.GetEmailStore().GetEmailAsync(user).ConfigureAwait(false);
My question is - how can this work in AspNet.Identity framework, but not in my project? It doesn't look like that Manager is implementing IUserEmailStore.
All I tested, was default MVC template in VS2013
I finally found the answer (after searching a lot).
If you see the UserManager's source code, the GetEmailStore() method does exist, but is internal. Take a look:
internal IUserEmailStore<TUser, TKey> GetEmailStore()
{
var cast = Store as IUserEmailStore<TUser, TKey>;
if (cast == null)
{
throw new NotSupportedException(Resources.StoreNotIUserEmailStore);
}
return cast;
}
So, it exists only inside files of the own Identity assembly, that's why it works there and not in your code.
Moreover, you can achieve the same result as follow:
var email = await Manager.GetEmailAsync(user.Id).ConfigureAwait(false);
Click here to see UserManager's Source Code

ServiceStack Ormlite System.Data.IDbConnection' does not contain a definition for 'From'

i was working on a console application and the word "From" wasn't a problem
ex var Best = db.Select<TopSellingGraph>(
db.From<products>
.Join<SalesOrderDetail>());
but when i start to use the servicestack api i always go into this problem
the error message is Error 1 'System.Data.IDbConnection' does not contain a definition for 'From' and no extension method 'From' accepting a first argument of type 'System.Data.IDbConnection' could be found (are you missing a using directive or an assembly reference?)
and i put in the apphost this code
var conString = ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
var conFactory = new OrmLiteConnectionFactory(conString, SqlServerDialect.Provider, true);
container.Register<IDbConnectionFactory>(c => conFactory);
i did exactly like the git-hub course
https://github.com/ServiceStack/ServiceStack.OrmLite
anyone have any idea ?
Most of OrmLite's APIs are extension methods over ADO.NET's IDbConnection interfaces which are made available when using the ServiceStack.OrmLite namespace:
using ServiceStack.OrmLite;
Tools like ReSharper help identify, add and can alleviate the burden of dealing with namespaces in C#.

Categories