我的模型类中有一个属性用户名,我想对其进行验证以限制用户输入任何空格或逗号。目前,它仅使用以下正则表达式来限制空白,但我也想限制逗号。请建议
[Required]
[Display(Name = "UserName")]
[RegularExpression(@"^\S*$", ErrorMessage = "Username Cannot Have Spaces")]
public string UserName { get; set; }
Run Code Online (Sandbox Code Playgroud) 我正在研究在 React.js 客户端应用程序和建立在关系 SQL 数据库之上的服务器应用程序之间使用 GraphQL 的可能性。应在客户端创建查询,包括复杂的 SQL 样式语句,例如:
WHERE Customer.Age BETWEEN 22 AND 25
AND Order.Status = 'Active'
OR Product.Name LIKE '%foo%'
Run Code Online (Sandbox Code Playgroud)
这意味着客户端通常应该只接收一小部分记录(例如 10 而不是 10M)。
这篇看起来不错的Phil Sturgeon 文章宣称了一些奇怪的事情:
我希望 GraphQL 可以帮助客户定义他们自己的范围,将这些包含过滤为适当的数据本身,这将有助于识别 API 应该添加为方便方法的范围包含。
在这种情况下,GraphQL 似乎对 API 开发人员没有帮助,但似乎确实有人谈论在未来添加 @filter 来做到这一点。
在将来?现在 GraphQL 中没有过滤?我继续研究并发现了这个 SO 问题和这个惊人的交互式 Graphcool 文档。这两个示例都使用了一个filter由一组后缀调用的功能,例如_gte:
query combineMovies {
allMovies(filter: {
OR: [{
AND: [{
releaseDate_gte: "2009"
}, {
title_starts_with: "The Dark Knight"
}]
}, {
title: "Inception" …Run Code Online (Sandbox Code Playgroud) 我有ASP.NET Core API.我已经阅读了这里的文档,展示了如何在asp.net核心中进行集成测试.该示例设置测试服务器,然后调用控制器方法.
但是我想直接测试一个特定的类方法(不是控制器方法)?例如:
public class MyService : IMyService
{
private readonly DbContext _dbContext;
public MyService(DbContext dbContext)
{
_dbContext = dbContext;
}
public void DoSomething()
{
//do something here
}
}
Run Code Online (Sandbox Code Playgroud)
当测试开始时,我希望startup.cs被调用,以便所有依赖项都会被注册.(比如dbcontext)但是我不确定在集成测试中如何解决IMyService?
注意:我想DoSomething()直接测试方法的原因是因为任何控制器都不会调用此方法.我Hangfire在此API中使用进行后台处理.Hangfire的后台处理作业将调用DoSomething()方法.因此,对于集成测试,我想避免使用Hangfire并直接调用DoSomething()方法
在Entity Framework 6中,我可以像这样获得类(myModel)的ModelMetadata:
var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, myModel.GetType());
Run Code Online (Sandbox Code Playgroud)
在.net core 1.1.1中我该怎么做?
我正在使用NLog.Logging.Extensions编写一个asp.net核心应用程序来提供日志记录.
日志注册:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
loggerFactory.AddNLog();
loggerFactory.ConfigureNLog("nlog.config");
loggerFactory.AddConsole();
loggerFactory.AddDebug();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
我正在获取日志输出,但它与.config文件中定义的日志记录布局的格式不匹配,并且它不会显示以下信息(但同样,它配置为在配置文件中显示跟踪及以上).
是否有人能够阐明为什么会出现这种情况?
nlog.config:
<?xml version="1.0" encoding="utf-8"?>
<nlog>
<variable name="Layout" value="${longdate} ${level:upperCase=true} ${message} (${callsite:includSourcePath=true})${newline}${exception:format=ToString}"/>
<targets>
<target name="debugger" type="Debugger" layout="${Layout}" />
<target name="console" type="ColoredConsole" layout="${Layout}" detectConsoleAvailable="False"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debugger,console" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
示例日志输出:
Hosting environment: Development
Content root path: /Users/###/dev/###/Services/src/app/###/###
Now listening on: http://localhost:8888 Application started.
Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:8888/State
info: ###.###.###[0]
Building ### …Run Code Online (Sandbox Code Playgroud) 我有一个ASP .Net Core 1.1 MVC Web API.如何在控制器操作中使用字符串路由约束?
我有以下两个动作:
/ GET: api/Users/5
[HttpGet("{id:int}")]
[Authorize]
public async Task<IActionResult> GetUser([FromRoute] int id)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
User user = await _context.User.SingleOrDefaultAsync(m => m.UserId == id);
if (user == null)
return NotFound();
return Ok(user);
}
// GET: api/Users/abcde12345
[HttpGet("{nameIdentifier:string}")]
[Authorize]
public async Task<IActionResult> GetUserByNameIdentifier([FromRoute] string nameIdentifier)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
User user = await _context.User.SingleOrDefaultAsync(m => m.NameIdentifier == nameIdentifier);
if (user == null)
return NotFound();
return Ok(user);
}
Run Code Online (Sandbox Code Playgroud)
第一个工作,但第二个不工作 - .Net不喜欢"字符串"约束.所以基本上,我再次执行HTTPGET请求:
c# routes asp.net-core-mvc asp.net-core-webapi asp.net-core-1.1
我有一个客户端(Xamarin)和两个Web API服务器A和B.客户端向A发出请求,使用请求参数向B发出另一个请求.如何将A从B接收的响应返回给客户端B使用C对客户来说是透明的.
例如,如果我使用HttpClient如何HttpResponseMessage在控制器的操作中转发到客户端,从A到B发出请求?
在尝试提交带有文件上传/多文件上传/表单字段/复选框的表单时,我收到上述错误.我在SO上经历了各种资源但无法确定问题.这是我的堆栈跟踪如下:
[ArgumentException: An item with the same key has already been added.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +10925834
System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) +10
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[] array, Func`2 keySelector, IEqualityComparer`1 comparer) +209
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +201
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +387
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +180
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +106
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +2541
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +633
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +494
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +199
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +1680 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将面板添加到名称为Tablica的现有面板中,但是我做错了错误,每次添加面板时都会增加全局变量,因此新添加的面板具有不同的Y位置,这会使我的面板彼此不重叠。但是现在我想使用一种不同的方法,以便我不将它们添加到具有固定的X,Y位置的位置,而是以某种方式将它们停靠在顶部,并且每个添加的新面板都停留在父面板的顶部,换句话说,最后一个面板添加了单击按钮时,它位于Tablica面板的顶部
这是我现在使用的代码,除了最后一个面板添加在面板底部之外,现在可以使用:
int TabliciLocation = 30; //global variable
private void OK_Click(object sender, EventArgs e)
{
Panel newPanel = new Panel();
newPanel.Size = new System.Drawing.Size(1200, 52);
newPanel.Location = new System.Drawing.Point(16, TabliciLocation);
Tablica.Controls.Add(newPanel);
TabliciLocation += 60;
}
Run Code Online (Sandbox Code Playgroud)
因此,新方法必须是这样的:
private void OK_Click(object sender, EventArgs e)
{
Panel newPanel = new Panel();
newPanel.Size = new System.Drawing.Size(1200, 52);
newPanel.Dock = DockStyle.Top; // if this can help
Tablica.Controls.Add(newPanel);
}
Run Code Online (Sandbox Code Playgroud) 我的控制器中有以下JsonResult:
public JsonResult SearchRxNormDrugs(string term)
{
var matches = rxnConsoService.SearchRxNormDrugs(term);
return Json(matches, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
在调试项目时,我可以看到它matches有300多个结果.所有比赛都有内容.但是,当我在url中导航到此操作时,我得到的结果如下:
模型:
public class RxNConso
{
[KeyProperty(Identity = true)]
string RXCUI { get; set; }
string LAT { get; set; }
string TS { get; set; }
string LUI { get; set; }
string STT { get; set; }
string SUI { get; set; }
string ISPREF { get; set; }
string RXAUI { get; set; }
string SAUI { get; set; } …Run Code Online (Sandbox Code Playgroud) 当我执行我的transact SQL代码时
IF (SELECT model_id FROM request_unit where request_id = '4357') IS NULL
SELECT part_id FROM request_unit WHERE request_id = '4357'
ELSE
SELECT model_id FROM request_unit where request_id = '4357'
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
子查询返回的值超过1.当子查询跟随=,!=,<,<=,>,> =或子查询用作表达式时,不允许这样做.
c# ×7
asp.net-core ×4
asp.net-mvc ×3
.net ×1
.net-core ×1
api-design ×1
asp.net ×1
coreclr ×1
graphql ×1
json ×1
nlog ×1
routes ×1
sql ×1
t-sql ×1
web-services ×1
winforms ×1
xunit ×1