我有以下资源用于返回特定的公司信息:
[HttpGet]
[Route("{companyId:Guid}")]
public IHttpActionResult Get(Guid companyId)
{
var company = CompanyRepository.Find(companyId);
if (company == null)
{
return NotFound();
}
else
{
var responseModel = new CompanyResponseModel()
{
CompanyId = company.Id,
Name = company.Name
};
return Ok(responseModel);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能将内容包含在NotFound()通话中?
例如,我想包含一个错误响应模型,其中包含"公司ID不存在"的消息.
这可能是针对RESTful设计的吗?
asp.net rest api-design http-status-code-404 asp.net-web-api2
我已将所有项目的输出路径更改为使用相同的位置。IE:

我的 ASP.NET Web API 项目现在在使用 IIS Express 在调试中运行时停止工作。
似乎 IIS Express 指向现有的 Web 应用程序项目目录:

这不应该现在指向..\Build\bin\Debug公共输出目录吗?我该如何配置?
c# visual-studio iis-express visual-studio-2013 asp.net-web-api2
我在Azure Web App中运行ASP.NET 5项目.
当使用大约1.5mb或更大的文件(表单数据)调用API端点时,我会502 Bad Gateway在响应主体中看到以下消息:
指定的CGI应用程序遇到错误,服务器终止了该进程.
奇怪的是,当上传较小的文件时,通话工作正常.它似乎产生了502大约1.5mb的标记,但它并不完全一致.
我正在使用ASP.NET 5 RC1.
在Startup.cs中,我app.UseIISPlatformHandler();作为方法中的第一个中间件添加Configure().
project.config:
{
...
"webroot": "wwwroot",
"dependencies": {
"Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
"Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Abstractions": "6.0.0-rc1-final",
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc1-final",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
"Newtonsoft.Json": "6.0.6",
"WindowsAzure.Storage": "5.0.2",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
...
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { }
},
}
Run Code Online (Sandbox Code Playgroud)
编辑:
正如Jessevl所建议的那样,抛出以下异常: …
在 VSTS(或 TFS 2015)中,$(DayOfYear)当低于 100 时,构建变量输出是否带有前面的零?
例如,它会输出063还是63?同样,003还是3?
我问的原因是因为我们使用以下内部版本号格式:
$(主要).$(次要).$(年份:yy)$(DayOfYear)$(修订版:.rr)
从技术上讲,如果前面没有零,1.0.16179.01(2016 年 6 月 27 日的构建)将被视为晚于1.0.173.01(2017 年 1 月 3 日的构建)的构建。如果前面有零,则该版本号将正确表示为1.0.17003.01。
我想实现自定义Content-Type验证过滤器,以便可以提供415 Unsupported Media Type上的自定义错误模型.
像这样的东西:
public class ValidateContentTypeFilterAttribute : ActionFilterAttribute
{
private const string JsonMimeType = "application/json";
public override void OnActionExecuting(ActionExecutingContext context)
{
string requestMethod = context.HttpContext.Request.Method.ToUpper();
if (requestMethod == WebRequestMethods.Http.Post || requestMethod == WebRequestMethods.Http.Put)
{
if (request.ContentType != JsonMimeType)
{
// "Unsupported Media Type" HTTP result.
context.Result = new HttpUnsupportedMediaTypeResult();
return;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是MVC管道似乎在执行任何自定义过滤器之前"捕获"不受支持或无效的Content-Type值.即使是'application/xml'内容类型也会被拒绝.
这将配置在哪里?
我的MVC配置包含的不仅仅是:
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling …Run Code Online (Sandbox Code Playgroud) asp.net-mvc-filters asp.net-core-mvc asp.net-core asp.net-core-1.0
我想将用户的"client_id"声明作为属性附加到发送到Application Insights的每个请求.
从我所读到的,我应该实现,ITelemetryInitializer但我需要HttpContext为请求,以检索"client_id".看我的初始化者:
public class ClaimTelemetryInitializer : ITelemetryInitializer
{
public HttpContext HttpContext { get; set; }
public void Initialize(ITelemetry telemetry)
{
this.AddTelemetryContextPropertFromClaims(telemetry, "client_id");
}
private void AddTelemetryContextPropertFromClaims(ITelemetry telemetry, string claimName)
{
if (HttpContext != null)
{
var requestTelemetry = telemetry as RequestTelemetry;
var claim = HttpContext.User.Claims.SingleOrDefault(x => x.Type.Equals(claimName, StringComparison.InvariantCultureIgnoreCase));
if (claim != null)
{
telemetry.Context.Properties[claimName] = claim.Value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以创建一个动作过滤器来每次设置上下文,但这感觉很糟糕:
public class TrackClaimsAttribute : ActionFilterAttribute
{
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ …Run Code Online (Sandbox Code Playgroud) 是否可以更改我的Application Insights服务的位置而不必删除并重新创建它?现在它已在RTM中,现在支持新的区域(例如北欧)。
有人可以向我解释为什么我的IEnumerable在GetMessages()上没有延迟加载.拿(5)?如果我调试foreach循环,它似乎延迟加载一个消息一次为前5个,将它们添加到listBox1,但然后在这5个之后它将继续填充列表的其余部分(这需要一分钟)在循环之后继续执行之前.
public void PopulateMessages()
{
foreach (string message in GetMessages().Take(5))
{
listBox1.Items.Add(message);
}
}
private static IEnumerable<string> GetMessages()
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
// The Message table has thousands of rows
SqlDataReader reader = new SqlCommand("SELECT * FROM Message", conn).ExecuteReader();
while (reader.Read())
{
yield return reader.GetString(0);
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试使用以下示例来理解枚举器
public class Garage : IEnumerable
{
private Car[] cars = new Car[4];
public Garage()
{
cars[0] = new Car() { Id = Guid.NewGuid(), Name = "Mazda 3", CurrentSpeed = 90 };
cars[1] = new Car() { Id = Guid.NewGuid(), Name = "Mazda 6", CurrentSpeed = 80 };
}
public IEnumerator GetEnumerator()
{
// return the array object's IEnumerator
return cars.GetEnumerator();
}
}
static void Main(string[] args)
{
IEnumerator i = cars.GetEnumerator();
i.MoveNext();
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going …Run Code Online (Sandbox Code Playgroud) 我在并行呼叫中做错了什么?
static void Main(string[] args)
{
TasteAll<HotCupOf>(TurnCoffeeVenMachineOn);
}
public static void TasteAll<Mode>(Action<Mode> Make)
{
foreach (Mode mode in Enum.GetValues(typeof(Mode)))
{
Task.Factory.StartNew(() => Make(mode) );
//Make(mode); //<-- Works Fine with normal call
}
Console.ReadLine();
}
enum HotCupOf
{
Black,
Latte,
Cappuccino,
Mocha,
Americano,
Espresso,
Chocolate,
Tea
}
public static void TurnCoffeeVenMachineOn(HotCupOf SelectedDrink)
{
Console.WriteLine(SelectedDrink);
}
Run Code Online (Sandbox Code Playgroud) c# ×4
.net ×2
asp.net-core ×2
azure ×2
api-design ×1
asp.net ×1
azure-devops ×1
dnx ×1
enums ×1
ienumerable ×1
iis-express ×1
lazy-loading ×1
linq ×1
rest ×1
tfs ×1
tfs-2015 ×1