我有以下模型类:
public abstract class CompanyFormViewModelBase
{
public CompanyFormViewModelBase()
{
Role = new CompanyRoleListViewModel();
ContactPerson = new PersonListViewModel();
Sector = new SectorListViewModel();
}
[Required]
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
public CompanyRoleListViewModel Role { get; set; }
[Display(Name = "Contact Name")]
public PersonListViewModel ContactPerson { get; set; }
public SectorListViewModel Sector { get; set; }
}
public class AddCompanyViewModel : CompanyFormViewModelBase, IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
PlugandabandonEntities db = new PlugandabandonEntities();
CompanyName = CompanyName.Trim(); …Run Code Online (Sandbox Code Playgroud) 我想从我的API方法返回无效的ModelState并尝试像在Web Api中一样使用它:
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Test");
Run Code Online (Sandbox Code Playgroud)
但它说,
严重性代码说明项目文件行抑制状态错误CS1929'HttpRequest'不包含'CreateErrorResponse'的定义,最佳扩展方法重载'HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage,HttpStatusCode,string)'需要类型为'HttpRequestMessage'的接收器
因此,ASP.NET Core中发生了一些变化,我应该将HttpRequestMessage作为第一个参数。但是,如何创建它以及为什么在响应中它是必需的呢?
编辑:
我发现,无效的模型可以通过以下代码返回:
return HttpBadRequest(ModelState);
Run Code Online (Sandbox Code Playgroud)
但无论如何,想知道灵活的方法来返回我自己的错误响应。
我想要一个表达式,找到值后的下一个符号不是“(”。
我有以下基本正则表达式:
(([_A-Za-z]([_\w])+)|([A-Za-z]))
Run Code Online (Sandbox Code Playgroud)
和文字,例如:
a3+red+42+_dv+Sy(w12+44)
Run Code Online (Sandbox Code Playgroud)
希望的正则表达式应返回:
a3, red, _dv, w12
Run Code Online (Sandbox Code Playgroud)
这个正则表达式返回
a3, red, _dv, Sy, w12
Run Code Online (Sandbox Code Playgroud)
但我需要排除“ Sy”,因为下一个符号是“(”。
我尝试以下方法:
(([_A-Za-z]([_\w])+)|([A-Za-z]))(\b)
Run Code Online (Sandbox Code Playgroud)
但它返回
a3+, red+, _dv+, w12)
Run Code Online (Sandbox Code Playgroud)
我不需要下一个符号,仅当下一个符号不是“(”时才需要包含。
我有一个包含 10.000 个文件的项目,我想将其推送到存储库。但在推送之前,我必须将它从 Unstaged 列表移到 Staged。愚蠢的问题,我没有看到像“全部移动”这样的按钮,它不理解拖放,我只能一个一个地移动文件?为什么以及如何解决这个奇怪的问题?
我是Jade Engine的初学者,无法在属性内设置值.所以,我有一个类似的代码:
for job in jobs
tr
td
a(href="/job/= job.ID")= job.Title
Run Code Online (Sandbox Code Playgroud)
= job.Title显示正确,但我无法设置job.ID. 我需要链接href像/ job/12345,其中12345是job.ID. 怎么做?
我需要完全按用户选择保存街景图像(包括panoID,航向,俯仰和fov).我有以下代码:
panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'));
panorama.addListener('pano_changed', function () {
$('#panoID').val(panorama.getPano());
});
panorama.addListener('pov_changed', function () {
$('#heading').val(panorama.getPov().heading);
$('#pitch').val(panorama.getPov().pitch);
$('#fov').val(panorama.getZoom());
});
Run Code Online (Sandbox Code Playgroud)
问题是我想将缩放保存为fov值 https://developers.google.com/maps/documentation/streetview/intro(查看fov可选参数)
fov(默认为90)确定图像的水平视野.视野以度为单位表示,最大允许值为120.在处理固定大小的视口时,与设定大小的街景视图图像一样,视野本质上表示缩放,较小的数字表示更高的缩放级别.
我找到了一些"转化"信息 https://developers.google.com/maps/documentation/javascript/streetview#TilingPanoramas
但它告诉我,fov可以到180,但是上一个.链接告诉120值是最大值.为什么?当然,我可以找到转换的比率,但也许存在正常的方式(即全景返回Fov而不是缩放)?
而且,似乎,捕捉放大pov_changed不是最好的方法.有时缩放不能正确更新
我有 WebApi 应用程序,并将 UserID 添加到 ApplicationOAuthProvider 类中的令牌:
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
context.AdditionalResponseParameters.Add("ID", context.Identity.GetUserId<int>());
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)
现在我怎样才能在我的控制器方法中获取这个ID?
我尝试以下操作:
[Authorize]
public class ApiEditorialController : ApiController
{
public HttpResponseMessage GetEditorialRequests()
{
int id = HttpContext.Current.User.Identity.GetUserId<int>();
var r = Request.CreateResponse(HttpStatusCode.Accepted);
r.ReasonPhrase = "Cool!";
return r;
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到 NullReferenceException
int id = HttpContext.Current.User.Identity.GetUserId<int>();
Run Code Online (Sandbox Code Playgroud)
细绳....
更新: 看看下面的响应(来自 Francis Ducharme)只需覆盖 OnAuthorization 而不是创建私有构造函数:)
public class AuthorizeApiFilter : AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext) …Run Code Online (Sandbox Code Playgroud) 源类:
public abstract class ApplicationDriverEquipmentFormAbstractVM
{
[StringLength(256)]
public string Make { get; set; }
[StringLength(256)]
public string Model { get; set; }
[StringLength(256)]
public string Year { get; set; }
[StringLength(256)]
public string PlateNumber { get; set; }
[StringLength(256)]
public string CurrentMileage { get; set; }
}
public class ApplicationDriverEquipmentTractorFormVM : ApplicationDriverEquipmentFormAbstractVM
{
[StringLength(256)]
public string VINNumber { get; set; }
}
public class ApplicationDriverEquipmentTrailerFormVM : ApplicationDriverEquipmentFormAbstractVM
{
[StringLength(256)]
public string Length { get; set; }
}
public class ApplicationDriverEquipmentStraightTruckFormVM …Run Code Online (Sandbox Code Playgroud) 我阅读了以下文档:https : //docs.microsoft.com/zh-cn/azure/application-insights/app-insights-api-custom-events-metrics
有许多不同的API方法可以跟踪异常,跟踪跟踪等。
我有一个ASP.NET MVC 5应用程序。例如,我有以下控制器方法(由ajax调用):
[AjaxErrorHandling]
[HttpPost]
public async Task SyncDriverToVistracks(int DriverID)
{
if ([condition])
{
// some actions here
try
{
driver.VistrackId = await _vistracksService.AddNewDriverToVistrackAsync(domain);
await db.SaveChangesAsync();
}
catch (VistracksApiException api_ex)
{
// external service throws exception type VistracksApiException
throw new AjaxException("vistracksApiClient", api_ex.Response.Message);
}
catch (VistracksApiCommonException common_ex)
{
// external service throws exception type VistracksApiCommonException
throw new AjaxException("vistracksApiServer", "3MD HOS server is not available");
}
catch (Exception ex)
{
// something wrong at all
throw new …Run Code Online (Sandbox Code Playgroud) 我将旧的MVC 5应用程序移动到Core,旧的应用程序有代码:
public class ValidateApiModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (var key in actionContext.ModelState.Keys)
{
result.Add(key, String.Join(", ", actionContext.ModelState[key].Errors.Select(p => p.ErrorMessage)));
}
// 422 Unprocessable Entity Explained
actionContext.Response = actionContext.Request.CreateResponse<Dictionary<string, string>>((HttpStatusCode)422, result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,这意味着,如果模型状态无效,那么我们返回带有错误的字典和422状态代码(客户端的要求).
我尝试通过以下方式重写它:
[ProducesResponseType(422)]
public class ValidateApiModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (var key in context.ModelState.Keys) …Run Code Online (Sandbox Code Playgroud) c# ×5
asp.net-core ×2
javascript ×2
access-token ×1
asp.net ×1
automapper ×1
automapper-6 ×1
azure ×1
express ×1
git ×1
google-maps ×1
pug ×1
regex ×1
telemetry ×1
token ×1
validation ×1