我有一个奇怪的问题,并尝试各种各样的事情来实现这一点.
我在我的Web API项目中有一个反向代理委托处理程序,它连接到内部资源,文件等的拦截请求,从我们的外部站点到我们DMZ内部的内部站点......
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace Resources.API
{
public class ProxyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var routes = new[]{
"/api/videos",
"/api/documents"
};
// check whether we need to proxy this request
var passThrough = !routes.Any(route => request.RequestUri.LocalPath.StartsWith(route));
if (passThrough)
return await base.SendAsync(request, cancellationToken);
// got a hit forward the request to the proxy Web …Run Code Online (Sandbox Code Playgroud) 我刚开始将异步编程实现到我正在编写的一些方法中.
业务逻辑是接受入站调用,如果项目尚未在全局高速缓存中处理,或者如果是,则只需更新该条目,然后稍后将在代码中处理该条目.
然而,我看到一些奇怪的行为,我无法理解.我的单元测试向我的QueueProcessor方法提交了两个请求(每次调用之间有2秒的延迟).为了测试,我故意在处理请求时QueueProcessor将调用的另一个方法中使用task.delay.这模拟了一个真实世界的测试用例,我们在处理第一个请求时阻止了其他请求.
我在我的子方法范围内使用一个名为ProcessRoutine的局部变量.但由于某种原因,当第二次调用进入更新全局缓存时,仅在ProcessRoutine方法范围内的局部变量也会更改.
此外,第二个请求将只有更新全局缓存变量然后停止的动作逻辑.因此没有其他代码被触发.我通过我的日志证实了这一点.我只是不明白为什么ProessRoutine方法中传递的数据集可以通过这种方式更改.
public async Task<bool> QueueProcessor(RtcPluginModel_IncidentModel passInModel)
{
//Ensure the processing cache is instantiated
if (QueueGlobalVariables.processingCache == null)
{
QueueGlobalVariables.processingCache = new List<tempTicketData>();
}
try
{
tempTicketData ticketItem = (tempTicketData)passInModel;
ticketItem.timeStamp = DateTime.Now;
var checkItemExistsInProcessingCache =
QueueGlobalVariables.processingCache.Find(
x => x.PAName == passInModel.PAName);
if (checkItemExistsInProcessingCache != null)
{
var result = QueueGlobalVariables.processingCache.Remove( QueueGlobalVariables.processingCache.Find(
x => x.PAName == passInModel.PAName && x.recordId == passInModel.recordId));
QueueGlobalVariables.processingCache.Add(ticketItem);
logger.Trace("Stopping update branch of code as no further action needed at this point.");
}
else …Run Code Online (Sandbox Code Playgroud) 用户控制器中的MakeUser方法用于创建用户名和密码.
[HttpGet]
public string MakeUser(UserParameters p)
{
const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string pass = "";
Random r = new Random();
for (int i = 0; i < p.Number; i++)
{
pass += chars[r.Next(0, 62)];
}
string firstTwoo = p.Name.Substring(0, 2);
string firstThree = p.Surname.Substring(0, 3);
return "Your username is: " + firstTwoo + firstThree + "\nYour password is: " + pass;
}
Run Code Online (Sandbox Code Playgroud)
UserParameter类,用于将参数作为对象发送.
public class UserParameters
{
public int Number { get; set; }
public string Name { get; …Run Code Online (Sandbox Code Playgroud) 我正在研究ASP.NET 5 API,并且了解到,为了使API尽可能“轻松”,我们将Http动词用作方法名称。
我的问题是,如果我有多种方法可以做不同的事情,而所有方法都必须是HttpPost,会发生什么?
说,我有一种方法可以调用来更新用户的名字,也可以有另一种方法用于更新用户的城市。在这两种情况下,输入参数都是用户ID(GUID)和值为字符串的值。
[HttpPost("id")]
public void Post([FromRoute]id, [FromBody]firstName)
{
// Change user's first name
}
[HttpPost("id")]
public void Post([FromRoute]id, [FromBody]city)
{
// Change user's city
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何命名方法?
c# asp.net-web-api asp.net-web-api-routing asp.net-web-api2 asp.net-core
我使用WebApi上传文件,但是当我运行时
request.Content.ReadAsMultipartAsync(provider)
Run Code Online (Sandbox Code Playgroud)
文件已上传,但其文件名已完全更改.我读过一些关于它的说法是出于安全原因而自动制作的.无论如何,我想用真实的文件名存储文件.知道怎么做吗?
我遇到了一个关于JavaScript的问题.具体来说,我有一张桌子
<table class="table">
<tr>
<th>
<label>Select</label>
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Middle)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.DoB)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.StartDate)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr id="@Html.DisplayFor(modelItem => item.EmployeeId)" ondblclick="DoubleClickOnRow(this)">
<td>
<input id="@Html.DisplayFor(modelItem => item.EmployeeId)" type="checkbox" onclick="SelectCheckBox(this)"/>
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Middle)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DoB)
</td>
<td>
@Html.DisplayFor(modelItem …Run Code Online (Sandbox Code Playgroud) 我对异步Web API编程有非常基本的疑问.我想异步调用我的控制器中的SaveCaseSearch.但是呼叫通过DAL的各个层并最终调用DB.
这些连续调用是否也应该异步?
我对异步的世界很新,所以我可能已经犯了一些错误.如果有什么不对劲,请纠正我.
所以对于控制器,我正在做类似下面的事情:
/*Create API for Case*/
[HttpPost]
[Route("createcase")]
public IHttpActionResult PostCreateCase([FromBody] ARC.Donor.Business.Case.CreateCaseInput CreateCaseInput)
{
ARC.Donor.Service.Case.CaseServices cs = new ARC.Donor.Service.Case.CaseServices();
var searchResults = cs.createCase(CreateCaseInput);
List<CreateCaseOutput> searchOutputResults = (List<CreateCaseOutput>)searchResults;
if (!string.IsNullOrEmpty(searchOutputResults.ElementAt(0).o_case_seq.ToString()))
SaveCaseSearchDetails(SaveSearchInput); /*This should be called asynchronously*/
return Ok(searchResults);
}
Run Code Online (Sandbox Code Playgroud)
这个
SaveCaseSearchDetails
现在需要在异步模式下调用.所以我写了:
[HttpPost]
public async Task<IHttpActionResult> SaveCaseSearchDetails([FromBody] ARC.Donor.Business.SaveSearchInput SaveSearchInput)
{
ARC.Donor.Service.Case.CaseServices cs = new ARC.Donor.Service.Case.CaseServices();
var searchResults = await cs.saveCaseSearchDetails(SaveSearchInput);
}
Run Code Online (Sandbox Code Playgroud)
那如果这是正确的
连续的呼叫也应该是异步的吗?
现在他们是
public IList<Entities.Case.SaveCaseSearchOutput> saveCaseSearch(ARC.Donor.Data.Entities.Case.SaveCaseSearchInput SaveCaseSearchInput)
{
Repository rep = new Repository();
string strSPQuery …Run Code Online (Sandbox Code Playgroud) 我有一个api,其响应如下
"prop1":"SomeValu1",
"prop2":"SomeValue2" ,
"prop3
":null,
"prop4":"SomeValue4"
问题是,基于输入,一些属性将为null(预期行为),我不希望在响应中返回.像这样的东西(prop3不存在)
"prop1":"SomeValu1",
"prop2":"SomeValue2",
"prop4":"SomeValue4"
哪个属性为null基于运行时逻辑.任何想法我怎么能这样做?
我有下面的控制器和路线
[HttpGet]
[Route("getByEmail/{email:alpha}")]
public IHttpActionResult Get(string email)
{
var user = _userLogic.GetUserByEmail(email);
return Ok(user);
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时
localhost/WebApp/api/user/getByEmail/fsd@sd
Run Code Online (Sandbox Code Playgroud)
它返回404未找到
I've tried
localhost/WebApp/api/user/getByEmail/?email=fsd@sd
Run Code Online (Sandbox Code Playgroud)
但它会得到所有可以有人请帮助我吗?
我在WebApi项目中使用了logging.cs类来记录有关传入请求的信息。
该类是从Global.asax中的Application_BeginRequest()方法触发的。 我需要在请求中获取Controller名称和Authorization标头。怎么做 ?
我一直在使用HttpRequest Request对象,但是似乎无法充分利用我真正需要的东西。(尽管Http请求的方法似乎很简单!!)
对于Authorization标头,我认为它就像“ Request.Headers [” Authorization“];”一样简单。但是,当前返回为null。
我的代码在下面,任何方向或建议将不胜感激。-杰森
namespace WCAPI.BLL
{
public class logging_2
{
private static HttpRequest Request
{
get { return HttpContext.Current.Request; }
}
private static HttpResponse Response
{
get { return HttpContext.Current.Response; }
}
public static void LogWcapiRequest(BLL.DebugTmr tmr)
{
if (tmr.EventType == DebugTmr.EventTypes.Null) { return; }
try
{
Services.AFSILog log = new Services.AFSILog();
log.Level = Services.LogLevels.Info;
log.SourceSystem = ANACore.AMTConfig.GetConfigurationValue("ConsoleLogSourceSystem");
if (Request.Url.IsLoopback) { log.SourceSystem += "_" + System.Environment.MachineName; }
log.Stamp = DateTime.Now;
log.Message = …Run Code Online (Sandbox Code Playgroud) asp.net-web-api ×10
c# ×8
asp.net ×4
asynchronous ×2
.net ×1
api ×1
asp.net-core ×1
asp.net-mvc ×1
async-await ×1
file-upload ×1
getmethod ×1
iis ×1
javascript ×1
multipart ×1
rest ×1