HttpContext.Current.Server.UrlEncode
Run Code Online (Sandbox Code Playgroud)
它仅适用于.NET Framework.如何在ASP.NET Core项目中编码或解码uri参数?
var range = (first: 1, end: 10);
Run Code Online (Sandbox Code Playgroud)
构建项目时,"错误列表"窗口中没有错误.但输出窗口写道:
1> .... cs(41,38,41,39):错误CS1026 :)预期
1> .... cs(41,40,41,41):错误CS1001:预期的标识符
1> .... cs(41,40,41,41):错误CS1002 :; 预期
1> .... cs(41,41,41,42):错误CS1002 :; 预期
1> .... cs(41,41,41,42):错误CS1513:}预期
==========重建全部:0成功,1失败,0跳过==========
只有当我删除代码
var range = (first: 1, end: 10);
时,此项目才能成功.
顺便说一下,我使用的是.NET Framework 4.6.2(不是.Net Core),我已经安装了这个软件包 System.ValueTuple
我想自动记录每个请求.在之前的.Net Framwork WebAPI项目中,我曾经注册了一个delegateHandler来执行此操作.
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.MessageHandlers.Add(new AutoLogDelegateHandler());
}
Run Code Online (Sandbox Code Playgroud)
AutoLogDelegateHandler.cs
public class AutoLogDelegateHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var requestBody = request.Content.ReadAsStringAsync().Result;
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
HttpResponseMessage response = task.Result;
//Log use log4net
_LogHandle(request, requestBody, response);
return response;
});
}
}
Run Code Online (Sandbox Code Playgroud)
日志内容的示例:
------------------------------------------------------
2017-08-02 19:34:58,840
uri: /emp/register
body: {
"timeStamp": 1481013427,
"id": "0322654451",
"type": "t3",
"remark": "system auto reg"
}
response: {"msg":"c556f652fc52f94af081a130dc627433","success":"true"}
------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
但是在.NET Core WebAPI项目中, …
服务器端:
public override Task OnConnected()
{
var connectionId = Context.ConnectionId;
var user = Context.User.Identity.Name; // Context.User is NULL
return base.OnConnected();
}
Run Code Online (Sandbox Code Playgroud)
客户端(在Console项目中):
IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();
Run Code Online (Sandbox Code Playgroud)
当客户端连接到服务器时,我想知道userName和connectionId之间的映射,但是Context.User
为NULL。如何在客户端设置此值?
class="tab-title"
v-on:click="tab"
v-for="(tabTitle,index) in tabTitleList"
:id="index"
Run Code Online (Sandbox Code Playgroud)
我在vue社区中找到了这个例子,但在我的情况下,我希望我的"id"有一个前缀,而不仅仅是一个数字.
也就是说,如果索引是1,我想要 <span id='sp_1'></span>
而不是<span id='1'></span>
.
这里文档仅展示了一种使用以下方式观察整个反应对象的方法
const state = reactive({
id: 1,
name: "",
});
watch(
() => state,
(state, prevState) => { // ...
}
);
Run Code Online (Sandbox Code Playgroud)
如果我只想观看 的变化怎么办name
?
watch(state.name, (name, prevName) => {
// for example: send a request to serve side for occupation validation
});
Run Code Online (Sandbox Code Playgroud)
上面的代码是错误的。
$('#jstree_demo_div2').jstree({
'core': {
'data': {
"url": "tree.ashx?id=" + _id,
"dataType": "json" // needed only if you do not supply JSON headers
}
},
"checkbox": {
'visible': true,
'keep_selected_style': false,
},
"plugins": ["wholerow", "checkbox"]
});
Run Code Online (Sandbox Code Playgroud)
我需要更改url(或变量_id
将更改),然后刷新数据.但似乎存在缓存问题.
我监视了HTTP请求,请求参数_id
没有改变.
我试过了
'core': {
'data': {
"url": "tree.ashx?id=" + _id,
"cache":false, //????
"dataType": "json" // needed only if you do not supply JSON headers
}
},
Run Code Online (Sandbox Code Playgroud)
它不起作用.
顺便说一句,我的jsTree.js版本是3.0.8.
我的请求样本
http://localhost:8065/api/note
POST
content-type:application/json
request body: { "id" : "1234", "title" : "test", "status" : "draft"}
Run Code Online (Sandbox Code Playgroud)
应该是
{ "msg" : "ok", "code" : 1}
Run Code Online (Sandbox Code Playgroud)
那个行动
public async Task<IActionResult> Post([FromBody]NoteModel model)
Run Code Online (Sandbox Code Playgroud)
为了自动记录每个请求,我创建了一个属性来完成这项工作.该属性如下所示:(来自Microsoft Docs)
public class SampleActionFilterAttribute : TypeFilterAttribute
{
public SampleActionFilterAttribute():base(typeof(SampleActionFilterImpl))
{
}
private class SampleActionFilterImpl : IActionFilter
{
private readonly ILogger _logger;
public SampleActionFilterImpl(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<SampleActionFilterAttribute>();
}
public void OnActionExecuting(ActionExecutingContext context)
{
}
public void OnActionExecuted(ActionExecutedContext context)
{
_logger.LogDebug("[path]" + context.HttpContext.Request.Path);
_logger.LogDebug("[method]" + context.HttpContext.Request.Method);
_logger.LogDebug("[body]"); …
Run Code Online (Sandbox Code Playgroud) 我已经构建了一个WebAPI,并希望创建一个单元测试项目来自动测试我的服务.
我的WebAPI流程很简单:
控制器(DI服务) - >服务(DI存储库) - > _repo CRUD
假设我有这样的服务:
public int Cancel(string id) //change status filed to 'n'
{
var item = _repo.Find(id);
item.status = "n";
_repo.Update(item);
return _repo.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
我想构建一个单元测试,它只使用InMemoryDatabase.
public void Cancel_StatusShouldBeN() //Testing Cancel() method of a service
{
_service.Insert(item);
int rs = _service.Cancel(item.Id);
Assert.Equal(1, rs);
item = _service.GetByid(item.Id);
Assert.Equal("n", item.status);
}
Run Code Online (Sandbox Code Playgroud)
我搜索了其他相关问题,发现了
您不能在测试类上使用依赖注入.
我只是想知道是否有任何其他解决方案可以实现我的单元测试理念?
我在控制台项目上使用它。
.NET框架:4.5
在我的测试代码中,onChange
尽管数据库中没有数据更改,但 SQLDependency 始终会触发。
class Program
{
private static string _connStr;
static void Main(string[] args)
{
_connStr = "data source=xxx.xxx.xx.xx;User Id=xxx;Password=xxx; Initial Catalog=xxx";
SqlDependency.Start(_connStr);
UpdateGrid();
Console.Read();
}
private static void UpdateGrid()
{
using (SqlConnection connection = new SqlConnection(_connStr))
{
using (SqlCommand command = new SqlCommand("select msgdtl,msgid From NotifyMsg", connection))
{
command.CommandType = CommandType.Text;
connection.Open();
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
SqlDataReader sdr = command.ExecuteReader();
Console.WriteLine();
while (sdr.Read())
{
Console.WriteLine("msgdtl:{0}\t (msgid:{1})", sdr["msgdtl"].ToString(), sdr["msgid"].ToString());
}
sdr.Close(); …
Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-core ×6
.net-core ×3
vue.js ×2
asp.net ×1
c#-7.0 ×1
javascript ×1
jstree ×1
log4net ×1
signalr ×1
signalr-hub ×1
tuples ×1
typescript ×1
vuejs2 ×1
vuejs3 ×1
xunit.net ×1