小编Pyl*_*iev的帖子

单元测试带有伪HTTPContext的ASP.NET Web API控制器

我正在使用以下方法通过ASP.NET Web API控制器上传文件.

[System.Web.Http.HttpPost]
public HttpResponseMessage UploadFile()
{
    HttpResponseMessage response;

    try
    {
        int id = 0;
        int? qId = null;
        if (int.TryParse(HttpContext.Current.Request.Form["id"], out id))
        {
            qId = id;
        }

        var file = HttpContext.Current.Request.Files[0];

        int filePursuitId = bl.UploadFile(qId, file);
    }
    catch (Exception ex)
    {

    }

    return response;
}
Run Code Online (Sandbox Code Playgroud)

在我的单元测试中,我HTTPContext在调用操作之前手动创建了一个类UploadFile:

var request = new HttpRequest("", "http://localhost", "");
var context = new HttpContext(request, new HttpResponse(new StringWriter()));
HttpContext.Current = context;

response = controller.UploadFile();
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法为Form集合添加自定义值,因为它是只读的.我也无法改变Files收藏品.

有没有办法在单元测试期间添加自定义值Form …

c# asp.net unit-testing asp.net-mvc-4

25
推荐指数
1
解决办法
1万
查看次数

如何获得两个数字的百分比并从百分比计算

我正在开发一个需要从两个数字计算百分比的应用程序。dotnet 有一些方法可以做到这一点吗?我喜欢自己写这个计算。但不幸的是我数学不好。真的,如何获得数学中的百分比?例如,我的应用程序中有 10000 个请求,其中 400 个请求是针对我的应用程序的特殊部分。如何计算我的应用程序的一部分请求占请求的百分比?以及如何计算反向,例如,我知道 5% 的用户喜欢我的特殊帖子,如何计算喜欢该帖子的用户数量?谢谢你,对不起我的英语不好。

.net c# math percentage

6
推荐指数
1
解决办法
1万
查看次数

React-Router 活动类不适用于嵌套路由

我查看了几个 github 问题和类似的帖子,但我无法弄清楚。我这里有我的路线:

  <Router history={browserHistory}>
    <Route path='/' component={Main}>
      <IndexRoute component={Landing} />
      <Route path='login' component={LoginContainer} />
      <Route path='user' component={UserContainer} onEnter={checkAuth} >
        <Route path='home' component={HomeContainer} />
        <Route path='settings' component={SettingsContainer} />
        <Route path='doc_attributes' component={AttributesContainer} />
        <Route path='groups' component={GroupsContainer} />
        <Route path='rules' component={RulesContainer} />
      </Route>
      <Route path='/dropbox/auth_finish' onEnter={doDropbox} />
      <Route path='/box/auth_finish' onEnter={doBox} />
      <Route path='/googledrive/auth_finish' onEnter={doGDrive} />
      <Route path='/onedrive/auth_finish' onEnter={doOneDrive} />
    </Route>
  </Router>
Run Code Online (Sandbox Code Playgroud)

以下是感兴趣的链接:

<li><Link to='/user/home' activeClassName="activeLink"><i className="fa fa-home fa-3x fa-fw" aria-hidden="true"></i></Link></li>
<li><Link to='/user/settings' activeClassName="activeLink"><i className="fa fa-wrench fa-3x fa-fw" aria-hidden="true"></i></Link></li>
<li><Link to='/user/groups' activeClassName="activeLink"><i className="fa …
Run Code Online (Sandbox Code Playgroud)

javascript router jsx reactjs react-router

5
推荐指数
1
解决办法
1240
查看次数

在没有安装 Docker 的情况下拉取和推送 docker 镜像

我们需要将大量的docker镜像从Azure DevOps传输到私有容器注册表(该注册表无法访问Internet)。对于这个问题,有一个带有 Windows Server 的代理计算机,带有 Azure Cli 和对 Azure DevOps 的访问权限,但我们在那里安装 Docker 受到限制。

有没有办法在不安装 Docker 的情况下从 Azure DevOps 中提取 docker 映像并将其推送到另一个容器注册表中?也许有 Docker 的精简版或一些官方脚本。

docker

5
推荐指数
1
解决办法
739
查看次数

托管Linux预览代理上的已缓存Docker映像

我们正在使用VSTS Hosted Linux Preview代理在VSTS上构建docker映像。microsoft / aspnetcore-build映像用于构建asp.net核心应用程序。每次触发生成时,代理都会从注册表中提取microsoft / aspnetcore-build映像,这需要一些时间。我们希望通过指定预先缓存在代理上的特定映像来避免这种情况。

是否有已托管在Hosted Linux Preview代理上的容器映像列表?此类信息可用于Hosted VS2017代理,但不适用于Linux。

docker azure-devops

3
推荐指数
1
解决办法
454
查看次数

ASP.NET Core 禁用特定控制器的 CORS

我的应用程序中有一组不同的控制器(假设 A 和 B)。A 控制器的 CORS 需要为 B 控制器启用和禁用。我通过以下方式通过策略配置了 CORS:

配置服务方法:

services.AddCors(
    options =>
    {
        options.AddPolicy(
            "AllowCors",
            builder =>
                {
                    builder.AllowAnyOrigin().WithMethods(
                        HttpMethod.Get.Method,
                        HttpMethod.Put.Method,
                        HttpMethod.Post.Method,
                        HttpMethod.Delete.Method).AllowAnyHeader().WithExposedHeaders("CustomHeader");
                });
    });
services.AddMvcCore()
Run Code Online (Sandbox Code Playgroud)

配置方法

app.UseCors("AllowCors");
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)

A 控制器集具有 EnableCors 属性

[EnableCors("AllowCors")]
public class ControllerA1: Controller
Run Code Online (Sandbox Code Playgroud)

CORS 按预期适用于一组 A 控制器(通过浏览器测试)。然而,它也适用于 B 控制器!我什至尝试使用 DisableCors 属性显式禁用 CORS:

[DisableCors]
public class ControllerB1: Controller
Run Code Online (Sandbox Code Playgroud)

但是,无论如何都可以从 UI 请求 ControllerB1 控制器。

B1 控制器浏览器中的标头

要求

Provisional headers are shown
Origin: http://localhost:5000
Referer: http://localhost:5000/all
User-Agent: Mozilla/5.0 AppleWebKit Chrome/69 Safari/537
Run Code Online (Sandbox Code Playgroud)

回复

Request URL: http://XX.XX.XX.XX/getall
Request …
Run Code Online (Sandbox Code Playgroud)

c# cors asp.net-core-mvc asp.net-core

3
推荐指数
1
解决办法
2万
查看次数