如何获取使用WebClient从WebAPI控制器返回的Content-Disposition参数?
WebApi控制器
[Route("api/mycontroller/GetFile/{fileId}")]
public HttpResponseMessage GetFile(int fileId)
{
try
{
var file = GetSomeFile(fileId)
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(file));
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = file.FileOriginalName;
/********* Parameter *************/
response.Content.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("MyParameter", "MyValue"));
return response;
}
catch(Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
客户
void DownloadFile()
{
WebClient wc = new WebClient();
wc.DownloadDataCompleted += wc_DownloadDataCompleted;
wc.DownloadDataAsync(new Uri("api/mycontroller/GetFile/18"));
}
void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
WebClient wc=sender as WebClient;
// Try to extract the filename from …
Run Code Online (Sandbox Code Playgroud) c# webclient httpresponse content-disposition asp.net-web-api2
我在服务器上使用Web Api OData v4,在客户端上使用OData客户端代码生成器.它工作正常,但我不知道如何在客户端上测试代码.
在服务器上,我公开了一个"Levels"dbSet.
这是客户端上的代码段:
public class LevelViewer
{
public virtual ODataContainer Container{get;set;} //t4 template generated
public LevelViewer(ODataContainer container=null)
{
if(container==null)
{
Container=new ODataContainer(new Uri("http://blabla"));
}
}
//I want to test this (actually there are more things, this is an example)
public List<Level> GetRootLevels()
{
return ODataContainer.Levels.Where(l=>l.IsRoot).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我接受由T4模板生成的odata容器作为构造函数的参数,以便能够以某种方式模拟它.
单元测试,这是我丢失的地方:
[TestMethod]
public void LevelsVMConstructorTest()
{
List<Level>levels=new List<Level>();
levels.Add(new Level(){Id=1,LevelId=1,Name="abc",IsRoot=True});
IQueryable<Level>levelsIQ=levels.AsQueryable<Level>();
//?
var odataContainerMock=new Mock<ODataContainer>();
odataContainerMock.Setup(m=>m.Levels).Returns( I DON'T KNOW );
//I want to get here …
Run Code Online (Sandbox Code Playgroud) 我正在关注使用 ASP.NET Core 和 MongoDB教程创建 Web API
而且我缺少一些非常基本的东西,即如何序列化具有可能是“多类型”属性的 Json 对象。也就是说,这个属性可以是一个字符串,也可以是另一个对象。
这是教程中的原始 Json 示例:
{
"_id" : ObjectId("5bfd996f7b8e48dc15ff215d"),
"Name" : "Design Patterns",
"Author" : "Ralph Johnson"
}
Run Code Online (Sandbox Code Playgroud)
这是教程中的原始 POCO 模型:
public class Book
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string BookName { get; set; }
public string Author { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我在 WebApi 控制器上调用“发布”和“获取”操作时,原始 Json 模型被正确序列化为 Book 实例,保存到数据库中并按预期从 Web 服务中正确检索。
但是,我需要添加一个“MultiUse”属性,其中MultiUse
可以是字符串或对象,也可以是其他内容,例如:
{
"_id" : ObjectId("5bfd996f7b8e48dc15ff215d"),
"Name" : "Design …
Run Code Online (Sandbox Code Playgroud) 如果用户的日期格式为:
MM/dd/yyyy
Run Code Online (Sandbox Code Playgroud)
但是,如果日期格式不同,则设置日期参数后会自动“清除”日期参数,例如IT DISAPPEARS,这将失败
同样,如果您在该“结束日期”参数中输入值,该值将自动消失。
我尝试过的事情:
将报表的“语言”属性设置为:“ zh-cn”,User!Language和空白。
很明显,SSRS认为输入的日期无效,因此将其清除。
我需要做些什么才能使这项工作?
我想在Idea.Status =="已验证"时执行某些操作,但QuickConverter(1 - 2)不允许我使用以下任何一项:
Binding="{qc:Binding '$P==Verified',P={Binding Path=Idea.Status}}"
Binding="{qc:Binding '$P=="Verified"',P={Binding Path=Idea.Status}}"
Run Code Online (Sandbox Code Playgroud)
"已验证"是一个意外的令牌.期待白色空间.
无法将表达式标记为"$ P =已验证".你忘记了'$'吗?
如何告诉quickconverter和XAML我想要与字符串进行比较?
考虑这个例子:我手动将主体缩放调整为 60%,以便让 #content div 水平适合,(内容 div 不能环绕或滚动,内容不限于文本,它包含更多的 div、表格、跨度,等。在示例中,我使用文本只是为了演示溢出)
body{
zoom:60%
}
#content{
overflow-x: hidden;
white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)
<div id="content">
THIS CONTENT HAS TO FIT HORIZONTALLY ON SCREEN, THIS CONTENT CAN'T WRAP NOR SCROLL BLA BLA BLA BLA BLA BLA BLA
</div>
Run Code Online (Sandbox Code Playgroud)
如何将其缩小或缩小以自动适应屏幕宽度?你能给我指出一个例子或来源吗?
我已经阅读了关于 css @viewport 和 jquery ui scale funcion 的内容,但我无法让它工作。
(在 3、2、1 中投反对票...)
我需要将它展平为新系列中的一个项目.
输入将是IEnumerable集合.基类如下所示:
public class ConversionsResult
{
public int SiteId { get; set; }
public int TotalLeads { get; set; }
public int TotalCalls { get; set; }
public int TotalEmails { get; set; }
public int UniqueVisits { get; set; }
public int MetricYear { get; set; }
public int MetricMonth { get; set; }
public string DeviceCategory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
扁平化的类看起来像这样:
SiteId, MetricMonth, MetricYear SUM(TotalLeads), SUM(TotalEmails), SUM(UniqueVisits), CONCATENATE(DeviceCategory).
Run Code Online (Sandbox Code Playgroud)
笔记:
我可以完成另一条路线,但我想使用LINQ.我试图破解我找到的一些解决方案,但这是一团糟,所以我排除了我试过的噪音.
如何"获取"div"containerDIV"中更改的复选框?
视图:
@model MyModel
<div id="containerDIV">
<ul id="CheckBoxList">
@foreach (XObject t in MyModel.XCollection)
{
<li>
<input type="checkbox" value="@t.id"/>
</li>
}
</ul>
Run Code Online (Sandbox Code Playgroud)
在JavaScript(Jquery)方面,我有这个:
$('#containerDIV').on('change', '#CheckBoxList', function (event) {
var id = $(this).val(); // this gives me null
if (id != null) {
//do other things
}
});
Run Code Online (Sandbox Code Playgroud)
很明显,$this
不是复选框,它是div containerDIV
或checkBoxList
如何进入复选框的状态和值?
c# ×6
html ×2
asp.net-core ×1
asp.net-mvc ×1
css ×1
datetime ×1
httpresponse ×1
javascript ×1
jquery ×1
json ×1
linq ×1
mongodb ×1
moq ×1
odata ×1
odata-v4 ×1
reportviewer ×1
scale ×1
sql ×1
sql-server ×1
unit-testing ×1
viewport ×1
webclient ×1
wpf ×1
xaml ×1
zooming ×1