#inherhits in razor files with ASP.NET Core 3.1 - visual-studio

I am trying to use a custom base class for my razor files in ASP.NET Core 3.1 by using #inherits in the cshtml file. Unfortunately Visual Studio 2019 as well as msbuild do not recognize, that a custom base class is used. I need to use this within a website, that I want to port from .NET 4.6.1 to .NET Core 3.1. With .NET 4.6.1 and ASP.NET this is working.
Here is a really simple example. My base class looks like this:
// Really simple base class just for demonstration purposes
public abstract class ViewBase
{
public MyHtmlHelper Html { get; } = new MyHtmlHelper();
public abstract Task ExecuteAsync();
// Just to make the compiler happy
public void WriteLiteral(string v) { }
public void Write(string v) { }
}
public class MyHtmlHelper
{
public string Checkbox() => "";
}
Whereas my cshtml file looks like this:
#inherits Example.ViewBase
#this.Html.CheckBox()
The following image shows that Visual Studio does not understand that this view should be derived from the base class Example.ViewBase. The Property Html should be of type MyHtmlHelper, but it is showing IHtmlHelper<dynamic> instead.
Also the method Checkbox is not recognized correctly.
When compiling this, the following error is shown:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'expression' of 'IHtmlHelper.CheckBox(string, bool?, object)'
It seems to me that with ASP.NET Core 3.1 razor views are tied to specific base class (of MVC?). Is that correct? Is there a way to properly use a custom base class with support by the editor?

Related

How to register a model binder in web api?

I'm trying to register a custom model binder in Web Api, but can't seem to find the correct way to do it.
System.ArgumentException: 'The service type SimpleModelBinderProvider is not supported.'
WebApiConfig.cs
using System.Web.Http;
using System.Web.ModelBinding;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
var provider = new SimpleModelBinderProvider(typeof(CustomerIdentity), new CustomerIdentityModelBinder());
config.Services.Insert(typeof(SimpleModelBinderProvider), 0, provider);
...
}
}
I've tried SimpleModelBinderProvider and ModelBinderProvider.
What is the proper way to register a custom model binder in web api?
NOTE: I'm not using the ModelBinderAttribute on the class because it's in another assembly which would cause a circular-dependency (and because I don't like decorating classes with attributes).
NOTE: Starting to think this might be namespace related. The project has both MVC5 and WebApi2 in it.
Namespace Issue
I should have been using the namespaces:
// In the WebApiConfig.
using System.Web.Http.ModelBinding;
using System.Web.Http.ModelBinding.Binders;
// For the ModelBinder itself.
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
Instead of:
using System.Web.ModelBinding;
The code was near identical as well as the class names, which is why it was hard to track down and figure out.

Automapper in MVC Core 2.0 services.AddAutoMapper() behavior

I have a solution like this:
MVC Core 2.0 application <-> Business Class library <-> Domain class library
(ViewModel) <- P1 -> (Dto) <-P2-> (Domain entity)
I created Automapper profiles in each MVC and Business projects for mapping ViewModel<->Dto (P1) and Dto<->Domain entity (P2). P1 profile&map is in MVC project, P2 profile&map is in Business library.
I then made a xUnit test project which creates a Dto object and sends it to a Business Service, inside the unit test on init I call:
Business.App.AutoMapperConfiguration.Configure();
And this unit test works exactly as expected.
I then do the same (I even copy/pasted code from Unit test) in the MVC controller and I get an error in mapping Dto to Domain entity:
Unmapped members were found. Review the types and members below...
I configured Automapper maps in startup.cs like this:
services.AddAutoMapper();
If I understand correctly this is supposed to traverse all assemblies for classes inheriting Profile and adding them to configuration.
Example map:
public class StrankaMap : Profile
{
public override string ProfileName => nameof(StrankaMap);
public StrankaMap()
{
CreateMap<SomeDto, SomeDomainEntity>().ReverseMap()
CreateMap<AnotherDto, AnotherDomainEntity>().ReverseMap()
}
}
I don't know what is the cause of this error if my unit test works but not from MVC app - I even copied the code from unit test to MVC controller and ran that. I'm suspecting an error in configuration. Do I assume correctly that inside Startup.cs adding services.AddAutoMapper(); is enough for this to work?
Solution (edit)
Apparently I misunderstood that the service.AddAutoMapper() will traverse all assemblies and search for Profile inherited classes. There might be a better solution but I used the one below, with the help of a hint from the comment #LucianBargaoanu.
I solved it like this:
// Startup.cs
services.AddAutoMapper(
typeof(Business.App.AutoMapperConfiguration),
typeof(MvcApp.Infrastructure.Configuration.AutoMapperConfiguration));
//And the AutoMapperConfiguration class:
namespace MvcApp.Infrastructure.Configuration
{
using AutoMapper;
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<Models.Mapping.StrankaMap>();
});
}
}
}
Apparently I misunderstood that the service.AddAutoMapper() will traverse all assemblies and search for Profile inherited classes. There might be a better solution but I used the one below, with the help of a hint from the comment #LucianBargaoanu.
I solved it like this:
// Startup.cs
services.AddAutoMapper(
typeof(Business.App.AutoMapperConfiguration),
typeof(MvcApp.Infrastructure.Configuration.AutoMapperConfiguration));
//And the AutoMapperConfiguration class:
namespace MvcApp.Infrastructure.Configuration
{
using AutoMapper;
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<Models.Mapping.StrankaMap>();
});
}
}
}

Xamarin WebView - Call C# Method

Is there a way in Xamarin's WebView that allows me to attach javascript events to my html elements and call C# method.
I could easily do this in Android by using JavaScriptInterface
<video width="320" height="240" controls="controls" poster='poster.gif'
onclick="window.JSInterface.startVideo('file:///sdcard/test.3gp');"
How would I manage to this in Xamarin
Create a JavaScript Interface Class
Create a C# class that contains methods to be called from JavaScript.
If you are targeting Android API level 17 or later, this
JavaScript-to-C# interface class must annotate each
JavaScript-callable method with [JavascriptInterface] and [Export]
as shown in the following example. If you are targeting Android API
Level 16 or earlier, this interface class must implement
Java.Lang.IRunnable as explained in Android API Level 16 and
Earlier (later in this recipe):
Create a C# class that is derived from Java.Lang.Object. In the following example, we name our class MyJSInterface and implement a
method to display a toast when it is called from JavaScript:
public class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
public void ShowToast ()
{
Toast.MakeText (context, "Hello from C#", ToastLength.Short).Show ();
}
}
Annotate each method that is to be exposed to JavaScript with [Export] and [JavascriptInterface] (see IJavascriptInterface
for more information about the JavascriptInterface annotation). In
the following example, the ShowToast method is annotated so that it
can be called from JavaScript. Note that you must include the
Java.Interop and Android.Webkit using statements as shown in this
example:
using Java.Interop;
using Android.Webkit;
...
[Export]
[JavascriptInterface]
public void ShowToast ()
{
Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
}
Add a project reference to Mono.Android.Export (so you can use the [Export] annotation):
1.In Visual Studio, right-click References in the Solution Explorer and select Add Reference.... In Xamarin Studio,
right-click References in the Solution Pad and select Edit
References....
2.In the search field, enter Mono.Android.Export. When you have located it, enable the check mark next to it and click OK.
Refer :
http://dotnetthoughts.net/how-to-invoke-c-from-javascript-in-android/
https://developer.xamarin.com/recipes/android/controls/webview/call_csharp_from_javascript/
https://developer.xamarin.com/samples/monodroid/WebViewJavaScriptInterface/
https://developer.xamarin.com/api/type/Android.Webkit.JavascriptInterface/

Implementing IntelliSense support for custom language (C++)

I wan to implement IntelliSense support for custom language. Actually it is a customize version of C++. i.e the methods resides in separate files
So my main class is like followings and it has import file MyClassMethods which has all the methods.
public class MyClass {
#import MyClassMethods
// my code goes here
}
So my MyClassMethods fiel looks like following and it has two methods,
public void testMethod1() {
}
public void testMethod2() {
}
Then at the end I want to have IntelliSense features when I working on MyClass. Example when I put dot character on that class in a required place I want to have testMethod1() and testMethod2() in the IntelliSense menu.
Is this possible to achieve and if so how can I achieve this?

Getting SNAP(AOP), NInject and ASP.Net MVC 3 working together

Has anyone got the SNAP AOP framework working with MVC 3 and Ninject.
The samples given when adding Snap using NuGet to an MVC 3 project don't specifcally work well with a previously added NInject package. I have tried to get it working based on the normal NInject approach but just cannot get it to actually intercept!
Can anyone show how to do this in code please?
I figured it out with the latest version of Ninject through NuGet which now adds a class call NinjectMVC3 in a new AppStart folder in the MVC3 application.
The code I used is as folows:
In the automatically created NinjectMVC3.cs CreateKernel() method:-
private static IKernel CreateKernel()
{
// Wire it up with AOP
NinjectAopConfiguration.NinjectAopConfigure();
//var kernel = new StandardKernel(); // Removed
RegisterServices(NinjectAopConfiguration._container.Kernel);
return NinjectAopConfiguration._container.Kernel;
}
I also wired up Ninject for the various injection targets in RegisterServices() method.
Next I took the sample code generated by NuGet when adding SNAP.Ninject to the MVC 3 application, renamed it NinjectAOP.cs and made it look like this:
public static class NinjectAopConfiguration
{
public readonly static NinjectAspectContainer _container;
static NinjectAopConfiguration()
{
_container = new NinjectAspectContainer();
}
public static void NinjectAopConfigure()
{
SnapConfiguration.For(_container).Configure(c =>
{
c.IncludeNamespace("MyNamespace.Model.*");
c.Bind<ExceptionLoggingInterceptor>().To<ExceptionLoggingAttribute>();
});
}
}
I also needed to do an assembly binding redirect for Ninject as follows because there is an assembly version conflict somewhere for Ninject:
I hope this helps someone.
I invite anyone to have a look and see if they can improve this please.

Resources