我正在使用自定义授权属性根据用户的权限级别授权用户访问权限.我需要重定向未经授权的用户(例如,用户尝试删除没有删除访问级别的发票)以访问被拒绝的页面.
自定义属性正在运行.但是在未经授权的用户访问的情况下,浏览器中没有显示任何内容.
控制器代码.
public class InvoiceController : Controller
{
[AuthorizeUser(AccessLevel = "Create")]
public ActionResult CreateNewInvoice()
{
//...
return View();
}
[AuthorizeUser(AccessLevel = "Delete")]
public ActionResult DeleteInvoice(...)
{
//...
return View();
}
// more codes/ methods etc.
}
Run Code Online (Sandbox Code Playgroud)
自定义属性类代码.
public class AuthorizeUserAttribute : AuthorizeAttribute
{
// Custom property
public string AccessLevel { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
string privilegeLevels = string.Join("", GetUserRights(httpContext.User.Identity.Name.ToString())); // Call another method to …Run Code Online (Sandbox Code Playgroud) 换句话说,如果每个类都没有("必须拥有")自定义属性(例如作者和版本),是否可以创建甚至不编译的程序集(假设检查代码未被删除)?
这是我在运行时查询时使用的代码:
using System;
using System.Reflection;
using System.Collections.Generic;
namespace ForceMetaAttributes
{
[System.AttributeUsage ( System.AttributeTargets.Method, AllowMultiple = true )]
class TodoAttribute : System.Attribute
{
public TodoAttribute ( string message )
{
Message = message;
}
public readonly string Message;
}
[System.AttributeUsage ( System.AttributeTargets.Class |
System.AttributeTargets.Struct, AllowMultiple = true )]
public class AttributeClass : System.Attribute
{
public string Description { get; set; }
public string MusHaveVersion { get; set; }
public AttributeClass ( string description, string mustHaveVersion )
{
Description = description; …Run Code Online (Sandbox Code Playgroud) 如果我通过MetadataType属性将属性应用于部分类,则不会通过Attribute.IsDefined()找到这些属性. 任何人都知道为什么,或者我做错了什么?
下面是我为此创建的测试项目,但我真的尝试将自定义属性应用于LINQ to SQL实体类 - 就像这个问题中的答案一样.
谢谢!
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace MetaDataTest
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] properties = typeof(MyTestClass).GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
// Displays:
// False
// False
// 0
}
Console.ReadLine();
}
}
[MetadataType(typeof(MyMeta))]
public partial class MyTestClass
{
public string MyField { get; set; }
}
public class MyMeta
{
[MyAttribute()]
public string MyField …Run Code Online (Sandbox Code Playgroud) 我正在尝试为Devise提供的User模型添加name属性.我在我的数据库中添加了一个"名称"列,并更改了注册视图,以便它询问用户的名字:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
Run Code Online (Sandbox Code Playgroud)
它允许我登录,但是当我这样做后检查数据库时,name: nil.我是否必须向Devise的User控制器添加一些东西?谢谢!
我知道如何遍历下面的输入,搜索具有特定类"测试者"的输入
以下是我的表现方式:
<input type='text' name='firstname' class="testing" value='John'>
<input type='text' name='lastname' class="testing" value='Smith'>
<script type="text/javascript">
$(document).ready(function(){
$.each($('.testing'), function() {
console.log($(this).val());
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
它按预期输出"John","Smith".
我想不使用class="testing"和使用自定义属性:testdata="John".
所以这就是我要做的事情:
<input type='text' name='firstname' testdata='John'>
<input type='text' name='lastname' testdata='Smith'>
Run Code Online (Sandbox Code Playgroud)
我的目标是使用内部的任何内容自动填充每个输入的值testdata,但只检测那些具有该testdata属性的值.
这是我尝试使用$.each循环的失败:
$.each($('input').attr('testdata'), function() {
var testdata = $(this).attr('testdata');
$(this).val(testdata);
});
Run Code Online (Sandbox Code Playgroud)
我得到了这样的答复:Uncaught TypeError: Cannot read property 'length' of undefined
谁能看到我做错了什么?
我只想放置与WEB API一起使用的条件必需属性
例
public sealed class EmployeeModel
{
[Required]
public int CategoryId{ get; set; }
public string Email{ get; set; } // If CategoryId == 1 then it is required
}
Run Code Online (Sandbox Code Playgroud)
我通过(ActionFilterAttribute)使用模型状态验证
为了避免在搜索已知类型的属性时使用过时的非泛型语法,通常会使用System.Reflection.CustomAttributeExtensions类中的扩展方法(从.NET 4.5开始).
但是,如果在重写方法的返回参数(或重写的属性/索引器的访问者)上搜索属性,则这似乎会失败.
我正在使用.NET 4.6.1体验这一点.
简单复制(完整):
using System;
using System.Reflection;
namespace ReflectionTrouble
{
class B
{
//[return: MyMark("In base class")] // uncommenting does not help
public virtual int M() => 0;
}
class C : B
{
[return: MyMark("In inheriting class")] // commenting away attribute does not help
public override int M() => -1;
}
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] // commenting away AttributeUsage does not help
sealed class MyMarkAttribute : Attribute
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用VS2008,并希望根据属性上的自定义属性创建编译时警告/错误(如果可能).
目前有两个案例让我感兴趣:
[MyAttribute (typeof(MyClass)]
Run Code Online (Sandbox Code Playgroud)
MyClass必须实现一个接口.目前我在属性的构造函数中声明了这一点,但由于堆栈跟踪的性质,这不容易跟踪:
public MyAttribute (Type MyClassType)
{
System.Diagnostics.Debug.Assert(typeof(MyInterface).IsAssignableFrom(MyClassType),
"Editor must implement interface: " + typeof(MyInterface).Name);
}
Run Code Online (Sandbox Code Playgroud)
我感兴趣的第二种情况是我在属性中定义了一个类型,如果该类型实现了一个接口,那么如果另一个属性不存在则应该显示警告.
IE if(MyClass.Implements(SomeInterface)&&!Exists(SomeAttibute)){Generate Warning}
[MyAttribute(typeof(MyClass)]
// Comment next line to generate warning
[Foo ("Bar")]
Run Code Online (Sandbox Code Playgroud)
谢谢!
有时我会为某些控件添加一个属性.喜欢:
<a href id="myLlink" isClimber="True">Chris Sharma</a>
Run Code Online (Sandbox Code Playgroud)
我知道这不是一个有效的HTML.但它在某些情况下对我有帮助.
这被认为是一种不好的做法吗?我的一位朋友说,它适用于Intranet环境,但在互联网上可能不会被搜索引擎发现友好.
如果这不是一个好习惯,那么最佳实践是什么?
谢谢
我正在使用Knockout-JS将我视图中的属性绑定到我的视图模型.Knockout-JS使用一个名为"data-bind"的自定义属性,您必须将该属性附加到要绑定到其中的控件以查看模型对象.
例:
<input type='text' name='first-name' data-bind='value: firstName'/>
Run Code Online (Sandbox Code Playgroud)
注意'data-bind'属性.
在我的视图渲染中,我在渲染具有此属性的文本框时遇到问题.我知道Html.EditorFor,Html.TextBoxFor和Html.TextBox助手都带有一个匿名对象,您可以使用它来指定自定义属性.这个实现的唯一问题是C#不允许破折号作为变量名,所以这不会编译:@Html.EditorFor(m => m.FirstName,new {data-bind ="value:firstName"});
我唯一能想到的是(在视图模型中):
public class DataBindingInput
{
public string Value { get; set; }
public string DataBindingAttributes { get; set }
}
public class MyViewModel
{
...
public DataBindingValue firstName { get; set; }
....
}
Run Code Online (Sandbox Code Playgroud)
以及一个名为"DataBindingInput.cshtml"的视图模板:
@model DataBindingInput
<input type='text' data-binding='@Model.DataBindingAttributes' value='@Model.Value'>
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我丢失了输入名称的自动生成,因此它不能用于回发,因为模型绑定器不知道如何绑定它.
我怎样才能做到这一点?
c# ×5
attributes ×3
.net ×1
devise ×1
each ×1
html ×1
jquery ×1
linq-to-sql ×1
metadata ×1
postsharp ×1
razor ×1
reflection ×1