Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an attribute so that I can decorate an action/controller and have some code run.
This is the code for my Attribute:
public class UpdateLastLogInAttribute : System.Attribute
{
Linq.UserMinData umd { get; set; }
public UpdateLastLogInAttribute()
{
this.umd =datafuncs.GetMinData();
if (umd != null)
datafuncs.SaveLastConnected(umd);
}
}
...and this is the Controller that I want the Attribute to work with
[funcs.UpdateLastLogIn]
public class HomeController : Controller
{
}
However, when I hit the controller, my code never executes. What's wrong?
Attributes are dumb. There isn't any magic that makes code in attributes run. It's down to you to reflect on your code and to detect the attributes and run the code.
Fortunately, MVC already does this for specific attribute types, so...
You might consider extending ActionFilterAttribute and overriding OnActionExecuting,OnActionExecuted, OnResultExecuting or OnResultExecuted depending on which phase of the request you want to intercept.
MVC looks out for subclasses of this attribute and executes the four methods above at the appropriate time.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have different Models in my Asp.Net Core application. Is it recommended to do SQL queries inside IActionResult Action methods? or is a better to have a seperate class do all work in terms of fetching data for each of the models and return the result to IActionResult method where we can display Return View()?
I have a static Utility class. If I were to have a class for each model to fetch data I would need access to IHttpContextAccessor but i cant assign that in a static class. What type of class would be the way to go?
You can go for the repository pattern (https://martinfowler.com/eaaCatalog/repository.html), adding a generic repository (one single class) or a concrete repository (one per model).
Another solution would be using partial classes, to add data access logic in your model classes.
In example:
public partial class MyModel
{
public string Name { get; set; }
}
public partial class MyModel
{
//add data access here
}
Also to identify them, you could add an extension to model class files, like ".cs.da" (then you would have "MyModel.cs" and "MyModel.cs.da" files) and nest them using File nesting extension: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
We have a project using net core. And my boss just told me that we will have another module called "User Access". So the first thing came to my mind is the controller for it. Can I name it UserAccessController or UseraccessController?
I just think it's weird to name a Controller with two CapitalLetter/CamelCase. Or i'm just used to one word controller like UserController, ReportController, etc.
My question is, is it ethical for developers to name a controller with two CamelCase? Thanks in advance.
Yes, it is acceptable, as long as the two (or more) words describe a noun.
class UserAccessController : Controller { }
class AccessHistoryController : Controller { }
class MailingAddressController : Controller { }
In your case, "Access" is being used as a noun and not a verb, so it's OK.
What is less acceptable is when the name contains a verb. The verb aspect should be a method name and kept out of the controller name.
//Bad
class DeleteUserController : Controller { }
//Better
class UserController : Controller
{
public async ActionResult Delete(UserDto user) { }
}
See Microsoft naming conventions, which say, among other things:
Names of Classes, Structs, and Interfaces
The naming guidelines that follow apply to general type naming.
✓ DO name classes and structs with nouns or noun phrases, using PascalCasing.
This distinguishes type names from methods, which are named with verb phrases.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I add the IdentityBasicAuthenticationAttribute in my project? I have read the link below shared by the #IonutUngureanu, but some of the steps are skipped in the document.
Please check the attached screenshot for the error.
Thank You.
You could use or create a custom Authentication/Authorize filter attribute:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
From your picture I see that you are missing the implementation of the IdentityBasicAuthenticationAttribute class. Step by step instructions:
Create a folder called Filters
In the folder Filters create a class called IdentityBasicAuthenticationAttribute :
public class IdentityBasicAuthenticationAttribute : BasicAuthenticationAttribute
{
protected override async Task<IPrincipal> AuthenticateAsync(string userName, string password, CancellationToken cancellationToken)
{
// Implement logic suitable for your case
return new ClaimsPrincipal(identity);
}
}
Register your filter:
config.Filters.Add(new IdentityBasicAuthenticationAttribute());
Full code sample here: http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/BasicAuthentication/BasicAuthentication/Filters/IdentityBasicAuthenticationAttribute.cs
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If a partial class has the same method name but different types I will get an error right?
public partial class Employee
{
public int sum()
{
}
}
public partial class Employee
{
public string sum()
{
}
}
Yes, it does:
'Employee' already defines a member called 'sum' with the same parameter types
Partial classes are just the same as regular class, except their definition is split in two sources. They have to comply to every rule regular classes have. That includes rules concerning the uniqueness of method signatures.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a solution created in C#. i have a piece of code in it base.\u002Ector();. When i try to build the application from Visual Studio I am getting the Following error.
Unexpected character '\u002E'
Remove \u002E and you're done!
002E is a full stop or just a dot ('.').
Your code must look like:
base.ctor();
It could happen because of transmitting a program source through different websites/programs with unsupported or broken encoding.
Moreover, ctor is a shortcut for constructor. Make sure you call constructor of base class correctly.
public class Animal
{
public Animal()
{
}
}
public class Cat : Animal
{
// next line will be converted to
// base.ctor() by compiler
public class Cat() : base()
{
}
}
Try to remove base.ctor(); in order to make your class work. I think it should work because your base class has no parameters in constructor.