I installed the Nuget package. hooked it through the "using" . The package classes work in another solution, and in my project they do not work
screenshots:
neolux
I think there are two separate issues here. I had an issue similar to #Leo in that the TestForNet() method is part of the NeoDB class.
Assuming that the method exists on the NeoRPC class for you, then #Leo is also spot on in suggesting that you can't create a variable (i.e. var api = NeoRPC.TestForNet()) within a class declaration as you have it in your screenshot.
If you'd like to set api when the class is created, you can create the variable at the class level and assign it in the constructor. I'm going to rely on NeoDB instead of NeoRTC in this example:
public class HomeController : Controller
{
private readonly NeoDB _api;
public HomeController()
{
_api = NeoDB.ForTestNet();
}
public IActionResult Index()
{
// _api.QueryRPC();
}
}
class from the attached library is not activated
You should put the code into the method rather than under the class directly, like:
public class HomeController : Controller
{
public ActionResult Index()
{
var api = NeoRPC.ForTestNet();
Hope this helps.
Related
I want to build a base controller that I can put some reusable methods so I do not have to put a bunch of repeat code in all my controllers. So I built a BaseController.cs
public class BaseController : Controller
{
public IHttpClientFactory _clientFactory;
public BaseController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
}
Then in one of my contollers I do public class TokenController : BaseController. But then it wants me to add the following but then it gives me errors
public TokenController(IHttpClientFactory clientFactory)
{
// I guess something goes here
}
But then VS Code tells me
There is no argument given that corresponds to the required formal parameter 'clientFactory' of 'BaseController.BaseController(IHttpClientFactory)' (CS7036)
What am I missing here? I been in JS world to long :)
When inheriting classes without default constructors you have to pass parameters to them using the following syntax:
public TokenController(IHttpClientFactory clientFactory) : base (clientFactory)
{
/* other initializations */
}
So add the following expression: : base (clientFactory)
See more information here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors
I am using Caliburn.Micro in combination with Xamarin.Forms. Within my App class, I register an interface ILicenseInfo with a class LicenseInfoImplementation via the SimpleContainer's PerRequest method.
CM then injects an object when my view model is created (see ViewModelOne) which is what I want. However, I don't see how I can extend this to a collection of objects. Lets say I would like CM to instantiate ViewModelTwo which expects a collection of object. How would I have to change App.cs and the XAML of ViewModelTwo to make that happen?
public partial class App : FormsApplication
{
private readonly SimpleContainer _container;
public App (SimpleContainer container)
{
InitializeComponent();
this._container = container;
// register interface and class
_container.PerRequest<ILicenseInfo, LicenseInfoImplementation>();
//....
}
}
public ViewModelOne(ILicenseInfo license)
{
// Great, Caliburn.Micro injects an LicenseInfoImplementation object
}
public ViewModelTwo(IEnumerable<ILicenseInfo> licenses)
{
// No idea
}
I finally used the following pattern. Not the most elegant way to do it but the best I found ...
public ViewModelTwo() : base (IoC.GetAll<ILicenseInfo>())
{
}
public ViewModelTwo(IEnumerable<ILicenseInfo> licenses)
{
// Do something with licenses
}
My theme has some sort of breadcrumb. The controller is always the category. To avoid repeat myself, I want to set it in the constructor of the controller for all actions like this:
class MyController:Controller{
public MyController() {
ViewBag.BreadcrumbCategory = "MyCategory";
}
}
When I access ViewBag.BreadcrumbCategory in the layout-view, its null. In a Action it works:
class MyController:Controller{
public IActionResult DoSomething() {
ViewBag.BreadcrumbCategory = "MyCategory";
}
}
I'm wondering that setting a ViewBag property is not possible in a constructor? It would be annoying and no good practice to have a function called on every action which do this work. In another question using the constructor was an accepted answear, but as I said this doesn't work, at least for ASP.NET Core.
There is an GitHub issue about it and it's stated that this is by design. The answer you linked is about ASP.NET MVC3, the old legacy ASP.NET stack.
ASP.NET Core is written from scratch and uses different concepts, designed for both portability (multiple platforms) as well as for performance and modern practices like built-in support for Dependency Injection.
The last one makes it impossible to set ViewBag in the constructor, because certain properties of the Constructor base class must be injected via Property Injection as you may have noticed that you don't have to pass these dependencies in your derived controllers.
This means, when the Controller's constructor is called, the properties for HttpContext, ControllerContext etc. are not set. They are only set after the constructor is called and there is a valid instance/reference to this object.
And as pointed in the GitHub issues, it won't be fixed because this is by design.
As you can see here, ViewBag has a dependency on ViewData and ViewData is populated after the controller is initialized. If you call ViewBag.Something = "something", then you it will create a new instance of the DynamicViewData class, which will be replaced by the one after the constructor gets initialized.
As #SLaks pointed out, you can use an action filter which you configure per controller.
The following example assumes that you always derive your controllers from Controller base class.
public class BreadCrumbAttribute : IActionFilter
{
private readonly string _name;
public BreadCrumbAttribute(string name)
{
_name = name;
}
public void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
var controller = context.Controller as Controller;
if (controller != null)
{
controller.ViewBag.BreadcrumbCategory = _name;
}
}
}
Now you should be able to decorate your controller with it.
[BreadCrumb("MyCategory")]
class MyController:Controller
{
}
I have the same issue and solve it overriding the OnActionExecuted method of the controller:
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
ViewBag.Module = "Production";
}
Here is a better way to do this for .NET Core 3.x, use the ResultFilterAttribute:
Create your own custom filter attribute that inherits from ResultFilterAttribute as shown below:
public class PopulateViewBagAttribute : ResultFilterAttribute
{
public PopulateViewBagAttribute()
{
}
public override void OnResultExecuting(ResultExecutingContext context)
{
// context.HttpContext.Response.Headers.Add(_name, new string[] { _value });
(context.Controller as MyController).SetViewBagItems();
base.OnResultExecuting(context);
}
}
You'll need to implement the method SetViewBagItems to populate your ViewBag
public void SetViewBagItems()
{
ViewBag.Orders = Orders;
}
Then Decorate your Controller class with the new attribute:
[PopulateViewBag]
public class ShippingManifestController : Controller
That's all there is to it! If you are populating ViewBags all over the place from your constructor, then you may consider creating a controller base class with the abstract method SetViewBagItems. Then you only need one ResultFilterAttribute class to do all the work.
i have a MVC controller called MyController with an action called MyAction. For other hand i have a Model called MyModel, and all this classes are in a project called Portal.Website (Asp.net MVC3 Application) that i use as a generic website and that store common functionalities for custom websites that i will add in the future.
For other hand i have another website project with a reference to Portal.Website project called Portal.Website.MyCustomWebsite.
This is the viewmodel MyModel.cs in the generic website part:
namespace Portal.Website
{
public class MyModel
{
[Required(ErrorMessage="The field Name is required.")]
[Display("MyPropertyOriginal")]
public virtual string Name{get;set;}
}
}
This is the controller and action in the generic website part:
namespace Portal.Website
{
public class MyController: Controller
{
[HttpPost]
public ActionResult MyAction(MyModel model)
{
if(Model.IsValid)
....
//My issue: Im getting the error message in english, not the overridden one.
}
}
}
This is the viewmodel that i created in the custom part:
namespace Portal.Website.MyCustomWebsite
{
public class MyModel: MyModel
{
[Required(ErrorMessage="My error message in other language.")]
[Display("MyPropertyOverriden")]
public override string Name{get;set;}
}
}
My problem:
I would like to override the ErrorMessage of the Required attribute. For this reason i created a new Model in my custom project. For other hand i would like to use the Controller/Action (MyController/MyAction) that is already defined in my common part.
Do you know if this is possible? Im only getting the issue with the Required attribute, but with the Display one its working perfect.
Thanks in advance.
Greets.
Jose.
You may want to check out this article that suggests two possible solutions :
http://www.codeproject.com/Articles/130586/Simplified-localization-for-DataAnnotations
I've found it was making more sense to re-create some DataAnnotation classes with my custom logic.
MVC3 comes with better support for I18N (internationalisation) than it's predecessors - you can pass the RequiredAttribute the type of your resource class and the resource key and the error message will be displayed in whichever language is most appropriate:
[Required(ErrorMessageResourceType = typeof(MyResources), ErrorMessageResourceName = "ResourceKey")]
public override string Name { get; set; }
I am trying to get my head around the sharp architecture and follow the tutorial. I am using this code:
using Bla.Core;
using System.Collections.Generic;
using Bla.Core.DataInterfaces;
using System.Web.Mvc;
using SharpArch.Core;
using SharpArch.Web;
using Bla.Web;
namespace Bla.Web.Controllers
{
public class UsersController
{
public UsersController(IUserRepository userRepository)
{
Check.Require(userRepository != null,"userRepository may not be null");
this.userRepository = userRepository;
}
public ActionResult ListStaffMembersMatching(string filter) {
List<User> matchingUsers = userRepository.FindAllMatching(filter);
return View("ListUsersMatchingFilter", matchingUsers);
}
private readonly IUserRepository userRepository;
}
}
I get this error:
The name 'View' does not exist in the current context
I have used all the correct using statements and referenced the assemblies as far as I can see. The views live in Bla.Web in this architecture.
Can anyone see the problem?
Thanks.
Christian
You should inherit UsersController from System.Web.Mvc.Controller class. View() method is defined in Controller class.
public class UsersController : Controller
{
//...
}