An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type - asp.net-mvc-4

I want to assign Group name as an attribute for authorize filter.
It will take as below
[FilterConfig.AuthorizeAd(Group = "DirectoryName")]
public ActionResult GetData()
{
}
Insted of hardcoding i tried by adding it as below
[FilterConfig.AuthorizeAd(Group = Constants.ActiveDirectoryName)]
Where Constants is class and created member as below:
public const string ActiveDirectoryName = "directoryName";
Now i want to aceess it from app.config, tried as below
[FilterConfig.AuthorizeAd(Group = ConfigurationManager.AppSettings["Directory_Name"].ToString()
It is throughing the error msg as "An attribute argument must be a constant expression"
How can i assign the data from config?
Please suggest me.

You can't do that with attributes, they have to be constants as stated in the error message. If you wanted to get a value from the configuration file, you could do it by passing the key to the attribute, and then in the constructor get the value you want from the configurationmanager
public MyAttribute :Attribute
{
private string _config;
public MyAttribute(string configKey)
{
_config = ConfigurationManager.AppSettings[configKey];
...
}
}
HTH

Related

asp.net core api - how to distinguish between `api/cars` and `api/cars?color=red` calls with [FromQuery] object

I am using [FromQuery] atribute in controller's Get method:
//CarsController, etc..
[HttpGet]
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery] CarsParameter? carsParam = null)
{
//param is always not null here
}
Inside the method I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null, so I cannot say if the Color="" (defailt value) is intended to be empty string or it's because of the call was api/cars
CarsParameter is a simple class:
public class CarsParameter
{
public string Color {get; set;} = "";
//more params here
}
Yes, I can use different path, like api/cars/withParams?color=red, but i am looking for more subtle solution.
I need to distinguish between api/cars and api/cars?color=red calls. Problem is, that carsParam object is never null
Please note that default model binding starts by looking through the sources for the key carsParam.Color. If that isn't found, it looks for Color without a prefix, which cause the issue.
To achieve your requirement, you can try to specify prefix explicitly, like below.
public async Task<ActionResult<IEnumerable<CarsDto>>> Get([FromQuery][Bind(Prefix = "carsParam")] CarsParameter? carsParam = null)
{
Request to api/cars?color=red&carsParam.color=yellow&carsParam.brand=test and following is test result

Out of Stack Space Error when setting Custom Object Parameter

I have a custom class module called Service with string parameters.
I instantiate the class by creating an object this_service like so:
Dim this_service As Service
Set this_service = New Service
Then I try to set a parameter to any string value like so:
this_service.Key = "HELLO"
When I run the macro I get the 28 Runtime Error, Out of Stack Space.
In my class module Service I have the following parameter definition and method calls:
Private pKey As String
Public Property Get Key() As String
Key = pKey
End Property
Public Property Let Key(Value As String)
Key = Value
End Property
I can't see any reason why I'd be getting this runtime error?
In Public Property Let it should be:
pKey = Value
Right now it calls the setter recursively (indefinitely).

A suitable constructor for type 'Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware' could not be located

A suitable constructor for type 'Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor
If this is the error you're getting it might be what I was hitting.
I was passing my extension a parameter that defaulted to null which was then being passed to UseMiddleware, e.g:
public static IApplicationBuilder UseMyCustomerMiddleware(this IApplicationBuilder builder, string myParam = null)
{
return builder.UseMiddleware<MyCustomMiddleware>(myParam);
}
It turns out that passing this null parameter does work so you'll want to change it to something non-null
...myParam = "")

C#, EF6 - Replace Required attribute by fluent api

I want to replace the [Required] attribute on the Gig property of the class Notification with the following fluent api expression.
public class NotificationConfiguration : EntityTypeConfiguration<Notification>
{
public NotificationConfiguration()
{
Property(n => n.Gig).IsRequired();
}
}
If I do so, the compiler throws an error CS0453:
The type 'Gig' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'StructuralTypeConfiguration'<Notification>.Property<T>(Expression<Func<Notification, T>>)'
I cannot see the reason, why this isn't working.
Thanks!
My mistake ;-)
Gig is an object with a table representation, so this must be a navigation property to that table instead of a required field. So the code looks like this:
public class NotificationConfiguration : EntityTypeConfiguration<Notification>
{
public NotificationConfiguration()
{
HasRequired(n => n.Gig);
}
}

RESTEasy mapping parameter with '-' in their name

A simple question (I hope so...) for RESTEasy experts.
I receive a form posted via POST which contains attributes with '-' in their names :
Example :
return-code=12
I want to map all the content of this POST into a Pojo :
public class MyFormInfo {
public String attr1="";
public String return_code=""; // don't work because return-code is not mapped in return_code
...
The method declaration is the following :
#POST
#Path("/return-cic-payment")
public String receiveForm(MyFormInfo form) throws Exception {
log.info("Return-code is : {}", form.return_code);
}
I don't to map attributes one by one in the parameters lists because the form contains a large number of fields.
Because I can't have an attribute named "return-code" in my POJO, I wonder how to do toget this parameter's value.
A custom mapping can be a solution, but I don't know how to achieve that.
Other idea I try without success, to receive a Map of attribute.
Thanks for your help.
Try this: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html_single/#_Form
class MyFormInfo{
#FormParam("return-code")
private String returnCode;
//etc.
}