custom-attributes

Any code that duplicates how the DebuggerDisplayAttribute generates the resulting string?

♀尐吖头ヾ 提交于 2019-12-04 18:45:50
问题 Anyone know of any code that duplicates how the DebuggerDisplayAttribute parses and gathers the resultant string? I would like to create a custom attribute that does nearly the sample thing. Similiar to "When a breakpoint is hit..." where you can use a variable within curly braces, as in "{variable}". I already handle simple cases, such as "{Name}", but something like "{Foo.Name}" requires extra reflection code that I need help with. Basically, I want to parse a string using the rules defined

Get list of custom attributes for current action/controller in ASP.NET MVC

白昼怎懂夜的黑 提交于 2019-12-04 17:45:41
问题 Checking out the sample code from http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc written for ASP.NET MVC2, I noticed they can check if a custom attribute is applied to the current action or controller by accessing filterContext.ActionDescriptor and filterContext.ActionDescriptor.ControllerDescriptor respectively: public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationContext

Custom Attribute above a controller function

故事扮演 提交于 2019-12-04 10:44:07
问题 I want to put [FirstTime] attribute above a controller function and then create a FirstTimeAttribute that has some logic that checks whether the user has entered his name, and redirects him to /Home/FirstTime if he hasn't. So Instead of doing: public ActionResult Index() { // Some major logic here if (...) return RedirectToAction("FirstTime", "Home"); return View(); } I would just do: [FirstTime] public ActionResult Index() { return View(); } Is this possible? 回答1: Sure. Do something like

How do I set custom HTML attributes in django forms?

两盒软妹~` 提交于 2019-12-04 10:10:20
问题 I have a Django form that is part of page. Lets say I have a field: search_input = forms.CharField(_(u'Search word'), required=False) I can access it only in template via {{ form.search_input }} . How to set custom HTML attrs (such as name and value)? I would like to find flexible solution, that would allow me to add any needed custom attributes to all types of fields. I found https://docs.djangoproject.com/en/dev/ref/forms/widgets/#widget But using attrs gives me (if used with CharField): _

Windows 7: Property Handler works in Explorer but Not FileOpenDialog?

给你一囗甜甜゛ 提交于 2019-12-04 09:34:53
问题 Working on writing a custom property Handler for our custom file type in windows 7. I have installed the Windows 7 SDK and built the sample Property Handler. After registering the handler, it works great in Windows Explorer, but in the common file open dialog the custom values do not appear. Does anyone know if there is something special I need to do to get the properties to appear in common dialogs? Explorer: File Open Dialog: 回答1: OK, figured it out. Here is the deal. My app is 32 bit and I

default login url on HttpUnauthorizedResult in asp.net mvc

好久不见. 提交于 2019-12-04 03:45:14
I have written a custom AuthorizeAttribute which has the following condition in asp.net mvc3 application: public override void OnAuthorization(AuthorizationContext filterContext) { //auth failed, redirect to Sign In if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new HttpUnauthorizedResult(); } } And in my web.config, i have: <authentication mode="Forms"> <forms loginUrl="~/User/SignIn" timeout="2880" /> </authentication> On authentication fail, it redirects to "/Account/Login" page by default. How do i change this default redirect url and redirect it to

Does using custom data attributes produce browser compatibility issues?

╄→гoц情女王★ 提交于 2019-12-04 01:59:24
I have to choose between custom data tags or ids. I would like to choose custom data tags, but I want to be sure that they do not cause browser compatibility issues for the most widely used browsers today. I'm using jQuery 1.6 and my particular scenario involves a situation where I need to reference a commentId for several actions. <div data-comment-id="comment-1" id="comment-1"> <a class="foo"></a> </div> It's easier to extract data tags in jQueryin: $('foo').data('commentId'); Extract a substring from the id seems a bit complicated and could break for one reason or another: <a id="comment-1"

How to plug method parameters into custom attribute

不想你离开。 提交于 2019-12-04 01:50:52
I have a custom Attribute called AuthoriseAttribute whose constructor looks like this: public AuthoriseAttribute(int userId) { .. blah } This is used with a method called GetUserDetails() like this: [Authorise(????????)] public UserDetailsDto GetUserDetails(int userId) { .. blah } At runtime, the presence of the Authorise attribute causes some authorisation code to execute which requires the ID of the user. Obviously, this can be extracted from the parameter of the GetUserDetails() method, but this means that the authorisation code depends on the method's parameter being given a particular

How to define named Parameters C#

纵然是瞬间 提交于 2019-12-03 23:33:23
问题 This seems like a simple question, but for some reason I can't find the answer anywhere. Basically, I'd like to be able to implement a constructor that takes NamedParameters . By named parameters, I do not mean parameters with default values (optional parameters) such as: public SomeMethod(){ string newBar = Foo(bar2 : "customBar2"); } public string Foo(string bar1 = "bar1", bar2 = "bar2" ){ //... } A good example of what I'm trying to achieve is the AuthorizeAttribute from the System.Web.Mvc

How to test that a method argument is decorated with an attribute?

烂漫一生 提交于 2019-12-03 22:15:41
This is probably a duplicate, but I can't find the question I'm looking for, so I'm asking it. How do you test that a method argument is decorated with an attribte? For example, the following MVC action method, using FluentValidation's CustomizeValidatorAttribute : [HttpPost] [OutputCache(VaryByParam = "*", Duration = 1800)] public virtual ActionResult ValidateSomeField( [CustomizeValidator(Properties = "SomeField")] MyViewModel model) { // code } I'm sure I'll have to use reflection, hopefully with strongly-typed lambdas. But not sure where to start. Tejs Once you get a handle on the method