Difference Between Static or NoneStatic Methods in ASP.NET MVC - asp.net

I need a method that return me some parameters in controllers this is implementation of it:
public List<Parameter> GetParameters(FormCollection collection) {
List<Parameter> parameters = new List<Parameter>();
List<string> parameterNames = new List<string>();
//Get Parameters Names and Values
return parameters;
}
I use this method in all of controllers, So I think about 3 option that I have to define it:
1-For any controller class define it in that controller like this:
public class ProductController : Controller {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
2-Define it in static class as static method:
public static class GeneralMethods {
public static List<Parameter> GetParameters(FormCollection collection) {
//
}
}
3-Define it as a None Static :
public class GeneralMethods {
public List<Parameter> GetParameters(FormCollection collection) {
//
}
}
which one is better? which one have better performance? or any other option for define methods that used in many controllers?
what is your suggestion?

There will be no performance impact in any of the three. (Though last approach will create separate object each time,it will be gracefully handled by GC).
approach 1: NO, as a standard practice we should not duplicate the code.
approach 2: YES, if your method depends only on the input parameter.
approach 3: YES, if you need to set up some instance variable and your method depends on them.
suggested approach: (approach 1+ approach 3)
If this method is common to all of your controller (or most), declare a base controller with this method and inherit all other controller from it.
Static methods will not be a problem as any variable declared with in a method are with in scope of the method.

Related

any work around to allow for an action result to accept a Abstract class as parameter

I have different configurations all inheriting from a base configuration that are customized in forms. I want all of these to be handled by a single action result.
[HttpPost]
public IActionResult Register(AbstractBaseConfig config)
{
...do some logic...
return View("../Home/Index");
}
However, this is not possible because you cannot base in abstract classes as a parameter to an action result. Is there any work around for this so I don't need a seperate action result for each configuration? (I still want each configuration to be it's own class, I only need access to the base class methods in the action result logic).
Basically you can't, and the reason is that MVC will try to do new AbstractBaseConfig() as part of the Data Binding process (which parses the URL or the Form Post and puts the results in a concrete object). And by definition, doing new AbstractBaseConfig() is impossible for an abstract class.
It also makes sense for other reasons, I will explain why.
You seem to expect that MVC can determine the class from the parameters that are being passed in. That is not how it works, in fact the opposite is true: the Action Method has to specify the exact class, and then the Binder will instantiate that exact class and try to bind its properties.
Suppose you had this:
public abstract class Thing { public int ID { get;set; } }
public class NamedThing : Thing { public string Name { get;set; } }
public class OtherThing : Thing { public string Name { get;set; } }
and suppose it would be allowed to use:
public IActionResult Register(Thing thing)
then what would you expect to be in thing after Data Binding: a Thing object with only the ID set? Or one of the other object types, with Name set, but how would MVC ever be able to know which class you meant?
So for all these reasons, this is not possible.
You could have a base class inherit the abstract class and all your classes inherit from that base class whilst having that base class as your parameter
Take for example
public abstract class ABase
{
public void stuff()
{
var stuff = string.Empty;
stuff = "hello";
}
public virtual void otherstuff()
{
var stuff = string.Empty;
stuff = "hello";
}
}
public class Base : ABase
{
//empty
}
public class Derived : Base
{
public void mystuff()
{
this.stuff();
}
public override void otherstuff()
{
// Custom code
}
}
public ActionResult Register(Base config)
{
}

Set ViewBag property in the constructor of a ASP.NET MVC Core controller

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.

How can I generate a random object that's available in all methods in a controller?

Here's the code that I have:
namespace WebRole.Controllers
{
[RoutePrefix("api/WordAPI")]
public class WordAPIController : BaseController
{
[AuthorizeRoles(ERole.Super, ERole.Admin, ERole.WordAdmin)]
[HttpPost]
[Route("Wapi/{source}")]
public IHttpActionResult Wapi(string source)
{
What I would like to do is to generate a random object every time that a method is called and then be able to use it in the method.
So I would like to do something like this:
Random random = new Random();
And have it available everywhere in my code.
Any suggestions on where I could place and use random so it's available to all methods?
A way to do this is to declare a class variable that's instantiated inside the controllers constructor.
namespace WebRole.Controllers
{
[RoutePrefix("api/WordAPI")]
public class WordAPIController : BaseController
{
private Random random;
public WordAPIController()
{
this.random = new Random();
}
[AuthorizeRoles(ERole.Super, ERole.Admin, ERole.WordAdmin)]
[HttpPost]
[Route("Wapi/{source}")]
public IHttpActionResult Wapi(string source)
{
Does this mean that every request to the controller use the same random? No, or most likely not. The controllers are created per request and a random is therefore created for each request as well.

Specifying #RequestHeader once for all controllers in Spring Boot app

I have a Spring Boot app with multiple controllers serving various REST methods. Each of the methods require that the same header parameter be defined. Is there a way to specify something like the following one time for all controller methods?
public ResponseEntity get(#RequestHeader(value="NAME", required = true) String name, ...) {
...
}
Thanks.
You can probably achieve this using #ModelAttribute, like this:
public class Something {
private name;
//...
}
#ModelAttribute("something")
public Something addSomething(#RequestHeader(value="NAME", required = true) String name) {
return new Something(name);
}
#RequestMapping("/something")
public ResponseEntity get(#ModelAttribute Something something) {
//...
}
You can implement the #ModelAttribute populating method in a single Controller or in a #ControllerAdvice class, in order to assist multiple controllers. See reference documentation.

Is there a better method than ListWrapper to bind a List<T> in a Spring MVC method?

In order to retrieve a list in a Spring MVC application I would like to write something like:
public String myMethod(#RequestParam("foo") List<FooUi> foos)
But the only solution I've found so far is the following :
public String myMethod(FooListWrapperUi fooListWrapperUi)
I don't like this solution because I have to write a wrapper each time I need to retrieve a list. In this example, the wrapper is the following :
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class FooListWrapperUi
{
private ArrayList<FooUi> fooList;
}
So my question is, is it possible to use something like the first solution or is it impossible and I need to write a wrapper?
Thanks.
You can accommodate your use case by creating your own HandlerMethodArgumentResolver:
public class FooUiResolver implements HandlerMethodArgumentResolver {
#Override
public boolean supportsParameter(MethodParameter methodParameter) {
return (methodParameter.getParameterType().equals(FooUi.class) ||
(methodParameter instanceof Collection<?> && ((ParameterizedType) methodParameter.getParameterType().getGenericSuperclass()).getActualTypeArguments()[0] == FooUi.class));
}
#Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest,
WebDataBinderFactory webDataBinderFactory) throws Exception {
// Create instances of FooUi by accessing requests parameters in nativeWebRequest.getParameterMap()
}
}
The actual implementation will depend on how you would create one or more FooUi instances from the request parameters or body. You then need to register FooUiResolver in your servlet config:
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){
argumentResolvers.add(new FooUiResolver());
super.addArgumentResolvers(argumentResolvers);
}
Once registered, you can use FooUi in your controller method arguments without RequestParam or any other annotation:
#RequestMapping(value = "/foo")
public String myMethod(List<FooUi> foos){}

Resources