我对ASP.NET MVC的理解是,对于授权,我应该使用类似的东西 -
public class IPAuthorize : AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext) {
//figure out if the ip is authorized
//and return true or false
}
Run Code Online (Sandbox Code Playgroud)
但在Web API中,没有AuthorizeCore(..).
有OnAuthorization(..)和MVC的一般建议是不使用OnAuthorization(..).
我应该在Web API中使用什么来进行自定义授权?
我在ASP.NET Web API应用程序中使用开箱即用的ValuesController
public class ValuesController : ApiController
{
// GET api/values
[Queryable(PageSize = 1)]
public IQueryable<string> Get()
{
return new string[] { "value1", "value2", "value3", "value4", "value5" }.AsQueryable();
}
}
Run Code Online (Sandbox Code Playgroud)
当我 get http://localhost/api/values?$inlinecount=allpages
这是回应
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>value1</string>
</ArrayOfString>
Run Code Online (Sandbox Code Playgroud)
我没有注释 config.EnableQuerySupport();
过滤,排序工作正常.
如果我尝试get http://localhost/api/values?$inlinecount=XXXXX获得异常,那么Web API应用程序似乎就知道了inlinecount
<Message>The query specified in the URI is not valid.</Message>
<ExceptionMessage>'xxx' is not a valid value for $inlinecount.</ExceptionMessage>
<ExceptionType>Microsoft.Data.OData.ODataException</ExceptionType>
Run Code Online (Sandbox Code Playgroud)
我肯定有Microsoft.AspNet.WebApi.OData包 - 这是包管理器控制台的输出
PM> Install-Package Microsoft.AspNet.WebApi.OData
Attempting to resolve dependency 'Microsoft.Net.Http (= 2.0.20710.0 …Run Code Online (Sandbox Code Playgroud) MSDN上的这篇文章指出,您可以根据需要使用尽可能多的try catch块,并且不会产生任何性能成本,因为不会引发任何实际异常.
因为我总是认为即使没有抛出异常,try-catch总是会受到很小的性能影响,所以我做了一点测试.
private void TryCatchPerformance()
{
int iterations = 100000000;
Stopwatch stopwatch = Stopwatch.StartNew();
int c = 0;
for (int i = 0; i < iterations; i++)
{
try
{
// c += i * (2 * (int)Math.Floor((double)i));
c += i * 2;
}
catch (Exception ex)
{
throw;
}
}
stopwatch.Stop();
WriteLog(String.Format("With try catch: {0}", stopwatch.ElapsedMilliseconds));
Stopwatch stopwatch2 = Stopwatch.StartNew();
int c2 = 0;
for (int i = 0; i < iterations; i++)
{
// c2 …Run Code Online (Sandbox Code Playgroud) public static class HttpRequestHelper
{
public static string RequestBody()
{
var bodyStream = new StreamReader(HttpContext.Current.Request.InputStream);
bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
var bodyText = bodyStream.ReadToEnd();
return bodyText;
}
}
Run Code Online (Sandbox Code Playgroud)
我打算从ActionFilters中调用它来记录传入的请求.当然可能有多个同时发出的请求.
这种方法可以吗?
我正在使用SQL Server 2008,并注意到缺少一个重要的存储过程.
如何找出删除存储过程的时间和人员.
我有一个控制器和方法,可以将用户添加到数据库.
我从Fiddler用请求标题调用它如下 -
Content-Type:application/xml
接受:application/xml
主持人:localhost:62236
内容长度:39
和请求机构 -
<User>
<Firstname>John</Firstname>
<Lastname>Doe</Lastname>
</User>
Run Code Online (Sandbox Code Playgroud)
这按预期工作,该方法被调用,用户对象在PostUser方法中处理.
public class UserController : ApiController
{
public HttpResponseMessage PostUser(User user)
{
// Add user to DB
var response = new HttpResponseMessage(HttpStatusCode.Created);
var relativePath = "/api/user/" + user.UserID;
response.Headers.Location = new Uri(Request.RequestUri, relativePath);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
我在它自己的类中执行模型验证
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
// Return the validation errors in the response body.
var errors = …Run Code Online (Sandbox Code Playgroud) 我在C#中构建标准的三层应用程序
1前端控制台应用程序/但我可能会将其更改为ASP.NET MVC网页
2业务逻辑层
3使用连接到SQL数据库的实体框架的数据层/但这可能会更改为Windows azure
主要目的是显示一些客户数据.
存储在数据库中的客户具有以下字段 -
CustomerID
Firstname
Lastname
DateOfBirth
Othervalue1
Othervalue2
Othervalue3
Creationdate
Updatedate
IsDisabled //this represents "deleted" customers i.e. the app will never use deleted customers, but I want to keep them in the database anyway
Run Code Online (Sandbox Code Playgroud)
在中间层,我只想要
CustomerID
Firstname
Lastname
DateOfBirth
Othervalue1
Othervalue2
Othervalue3
Updatedate
Run Code Online (Sandbox Code Playgroud)
在第一个应用程序的前端我只会显示
CustomerID
Firstname
Lastname
DateOfBirth
Run Code Online (Sandbox Code Playgroud)
从如何从数据层加载客户(可能会更改)并在中间层然后在表示层(可能会更改)中使用该客户的角度,如何正确实施n层应用?
我在哪里放置客户模型?我需要多个吗?我在某处需要ICustomer接口吗?
项目详情 该项目将由两个团队开发,一个位于美国,另一个位于东欧,将有四到五个团队成员.
这个项目不会使用遗留数据访问层.相反,我们将使用Entity Framework构建一个新的; 我们需要设计和构建一个将在所有新应用程序中使用的数据层(对于这个应用程序,我们只需要客户表和一个或两个表).其他项目将向该层添加其他表.
我正在使用DI来注入ICustomerRepository(请参阅此SO问题).但是会实现存储库和工作单元模式.
我关心的是适当地分离各层.我们将在未来几个月内添加许多新项目,新数据层将快速增长.我们还考虑在某个时候迁移到Azure,因此我希望能够在不必重写业务和前端层的情况下交换实体框架数据层.
在Toad中键入查询时,通常会显示一个智能感知滚动框以帮助我进行输入.
但有时这不会出现.是否有一个捷径(如在Visual Studio中)来实现它?
编辑我发现键映射是CTRL +.
对于像这样的查询
SELECT * FROM Person AS P
WHERE P. -- I expect intellisense to show me the columns.
-- Sometimes it does not and pressing the CTRL+. does nothing
Run Code Online (Sandbox Code Playgroud)
按CTRL +.远离查询会带来一长串的事情,例如 -
@@ CONNECTIONS @@ CPU_BUSY @@ CURSOR_ROWS @@ DATEFIRST @@ DBTS @@ ERROR
我有正确的快捷键吗?
问题是Toad无法弄清楚上面查询中的列名吗?
编辑2
非常奇怪的行为
如果我有
USE DB1
SELECT * FROM Person AS P
WHERE P.--I get the intellisense
Run Code Online (Sandbox Code Playgroud)
但如果我在编辑器中有以下内容 -
USE DB1
SELECT * FROM Person AS P
WHERE P.--I …Run Code Online (Sandbox Code Playgroud) 如何为HttpRequestMessage添加IP地址?
我正在为Web.API应用程序编写单元测试,我有一个AuthorizeAttribute来检查调用者的IP地址 -
string[] AllowedIPs = new string[] { "127.0.0.1", "::1" }
string sourceIP = "";
var contextBase = actionContext.Request.Properties["MS_HttpContext"] as System.Web.HttpContextBase;
if (contextBase != null)
{
sourceIP = contextBase.Request.UserHostAddress;
}
if (!string.IsNullOrEmpty(sourceIP))
{
return AllowedIPs.Any(a => a == sourceIP);
}
return false;
Run Code Online (Sandbox Code Playgroud)
我按如下方式构建测试请求 -
var request = CreateRequest("http://myserver/api/CustomerController, "application/json", HttpMethod.Get);
HttpResponseMessage response = client.SendAsync(request).Result;
Run Code Online (Sandbox Code Playgroud)
..
private HttpRequestMessage CreateRequest(string url, string mthv, HttpMethod method)
{
var request = new HttpRequestMessage();
request.RequestUri = new Uri(url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv));
request.Method = method;
return …Run Code Online (Sandbox Code Playgroud) 我想在我的模型中将一组文本框(小时,分钟和秒)绑定到TimeSpan.我不想在我的模型中添加小时,分钟和秒,我宁愿不将它们作为参数传递给action方法.
我发现了一些关于使用编辑器模板的讨论,但找不到一个好的例子.
c# ×7
asp.net-mvc ×3
architecture ×1
class ×1
http-headers ×1
httpcontext ×1
intellisense ×1
json ×1
odata ×1
odata-v3 ×1
performance ×1
post ×1
sql ×1
sql-server ×1
syslog ×1
toad ×1
unit-testing ×1
xml ×1