小编Mic*_*uso的帖子

ASP.net 5 Web API Post CreatedAtRoute始终返回500内部服务器错误

数据库有效.它实际上插入了新记录,但是当我使用CreatedAtRoute()时,我总是从客户端获得500回.为什么?

我的控制器获取:

[Route("api/[controller]")]
public class IngredientController : Controller
{
    private SimpleCookbookDbContext db { get; set; }

    public IngredientController(SimpleCookbookDbContext context)
    {
        db = context;
    }

    // GET: api/values
    [HttpGet]
    public async Task<IEnumerable<Ingredient>> Get()
    {
        return await db.Ingredients.ToListAsync();
    }

    // GET api/values/5
    [HttpGet("{id}", Name = "GetIngredient")]
    public async Task<Ingredient> Get(int id)
    {
        return await db.Ingredients.SingleOrDefaultAsync(i => i.Id == id);
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]Ingredient ingredient)
    {
        try
        {

            var res = await IM.CreateAsync(ingredient);

            if (!res.Success)
            {
                return HttpBadRequest(res.Errors);
            }

        }
        catch(Exception) …
Run Code Online (Sandbox Code Playgroud)

asp.net-web-api asp.net-core-mvc

8
推荐指数
0
解决办法
2196
查看次数

实体框架默认日期时间注释不会迁移

我想创建一个DateTime字段,以便它GETDATE()一旦到达数据库就会有一个默认值.所以我这样做了:

 public class Specialty
 {
     [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
     public DateTime? CreatedDate { get; set; }

     // other properties not shown for brevity.
 }
Run Code Online (Sandbox Code Playgroud)

所以我在PM>控制台中运行了Add-Migration.在Up函数中导致了这一点(并未显示所有行):

AddColumn("dbo.Specialty", "CreatedDate", c => c.DateTime());
Run Code Online (Sandbox Code Playgroud)

我明白c.DateTime(...)的可空是默认为真,所以我明白为什么没有指定.但是在哪里defaultValueSql: "GETDATE()"?

我知道我可以手动把它,但这似乎打败了目的,Add-Migration使[DatabaseGenerated(DatabaseGeneratedOption.Computed)]无用.我知道defaultValueSql的默认值不是"GETDATE()",因为当我查看SQL Management Studio中的列属性时它不会显示.

少了什么东西?名称"CreatedDate"是否违反了EF惯例?还有其他我应该参考的图书馆System.ComponentModel.DataAnnotations.Schema吗?

c# datetime entity-framework

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

如何在OpenCV(3.0.0)OCL中启动自定义OpenCL内核?

我可能会误用OpenCV作为官方OpenCL C++绑定的包装器,以便我可以启动自己的内核.

但是,OpenCV确实有Program,ProgramSource,Kernel,Queue等类,它们似乎告诉我,我可以使用OpenCV启动自己的(甚至是非基于图像的)内核.我无法在这些课程中找到文档,更不用说示例了.所以,到目前为止,我对它进行了一次尝试:

#include <fstream>
#include <iostream>

#include "opencv2/opencv.hpp"
#include "opencv2/core/ocl.hpp"

#define ARRAY_SIZE 128

using namespace std;
using namespace cv;

int main(int, char)
{
    std::ifstream file("kernels.cl");
    std::string kcode(std::istreambuf_iterator<char>(file),
        (std::istreambuf_iterator<char>()));

    cv::ocl::ProgramSource * programSource;
    programSource = new cv::ocl::ProgramSource(kcode.c_str());

    cv::String errorMessage;
    cv::ocl::Program * program;
    program = new cv::ocl::Program(*programSource, NULL, errorMessage);

    cv::ocl::Kernel * kernel;
    kernel = new cv::ocl::Kernel("simple_add", *program);
    /* I'm stuck here at the args. */

    size_t globalSize[2] = { ARRAY_SIZE, 1 };
    size_t localSize[2] = { ARRAY_SIZE, 1 };    
    kernel->run(ARRAY_SIZE, globalSize, localSize, …
Run Code Online (Sandbox Code Playgroud)

c++ opencv gpgpu opencl

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

使用AspNet.Security.OpenIdConnect分离Auth和资源服务器 - 受众?

AspNet.Security.OpenIdConnect.Server上的示例看起来像auth和资源服务器.我想把它们分开.我已经这样做了.

在auth服务器的Startup.Config中,我有以下设置:

app.UseOpenIdConnectServer(options => {

    options.AllowInsecureHttp = true;
    options.ApplicationCanDisplayErrors = true;
    options.AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.Issuer = new System.Uri("http://localhost:61854"); // This auth server
    options.Provider = new AuthorizationProvider();
    options.TokenEndpointPath = new PathString("/token");              
    options.UseCertificate(new X509Certificate2(env.ApplicationBasePath + "\\mycertificate.pfx","mycertificate"));

});
Run Code Online (Sandbox Code Playgroud)

我有一个AuthorizationProvider编写,但我不认为它与我当前的问题相关(但可能相关).在GrantResourceOwnerCredentials覆盖中,我对声明主体进行了硬编码,以便验证每个令牌请求:

public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsNotification context)
{
    var identity = new ClaimsIdentity(OpenIdConnectDefaults.AuthenticationScheme);

    identity.AddClaim(ClaimTypes.Name, "me");
    identity.AddClaim(ClaimTypes.Email, "me@gmail.com");
    var claimsPrincipal = new ClaimsPrincipal(identity);

    context.Validated(claimsPrincipal);
    return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)

在资源服务器上,我在其Startup.config中有以下内容:

app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString("/api")), branch =>
{
    branch.UseOAuthBearerAuthentication(options => {
        options.Audience = "http://localhost:54408"; // This resource …
Run Code Online (Sandbox Code Playgroud)

oauth asp.net-web-api openid-connect aspnet-contrib asp.net-core

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

ASP.net 5中的IApplicationBuilder.UseJwtBearerAuthentication扩展名在哪里?

我见过的例子表明IApplicationBuilder有一个扩展方法.UseJwtBearerAuthentication(Action<?> options).

这个SO问题和AspNet.Security.OpenIdConnect.Server示例服务器启动文件似乎说有这样的扩展.在OpenIdConnect上,我查看了extensions文件夹,但没有看到名为UseJwtBearerAuthentication的扩展名.甚至这个博客说它应该包含在ASP.net 5.我也尝试将Microsoft.AspNet.Security.OAuthBearer,1.0.0-beta3添加到我的project.json并在Startup.cs中引用它.没有帮助.

我唯一不同的是我正在使用-beta7,但我认为这不重要.

asp.net-web-api asp.net-core-mvc asp.net-core

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

如何从ASP.NET 5 Web Api返回HTTP 500?

控制器现在具有CreatedAtRoute()201、400 HttpBadRequest()等的功能。我看不到500的功能HttpInternalServerError()。

但是,有HttpStatusCodeResult一个我可以创建并返回的类:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
   ...
    try{
    }
    catch(Exception e)
    {
         return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想从中返回一些信息e。这对于现场直播来说可能是不好的做法,但是在进行测试时,我们希望从API调用返回的主体中查看错误。

HttpStatusCodeResult没有object用于提供元数据的type 或任何属性。

该怎么办?我不想返回400,因为这不是内部服务器错误的意思。

c# http-status-code-404 asp.net-web-api asp.net-core

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

当 TemplateRef 通过 @Input 传入时,Angular @ContentChildren QueryList 不会检测到更改

我将从我的组件在托管组件上的使用开始:

<my-templated-component [content-template]="contentTemplate">
</my-templated-component>

<ng-template #contentTemplate>
  <h3>Hello There</h3>
  <div>Lorem ipsum..... </div>
  <custom-component></custom-component>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

现在,我的组件的 .ts:

class MyTemplatedComponent implements AfterContentInit {
  @Input('content-template') contentTemplate: TemplateRef<any>;
  @ContentChildren(CustomComponent) customComponentQueryList: QueryList<CustomComponent>;

  ...
  ngAfterContentInit() {

    // Length is 0 at this point. My component didn't find the component I want
    console.log(`There are ${this.customComponentQueryList.length} items`);

    this.customComponentQueryList.changes.subscribe((s) => {
      /* This is not called. This is where I would have wanted to find
         my CustomComponent to perform a task that requires its presence.
      */
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

这是 …

templates angular2-template angular

5
推荐指数
0
解决办法
563
查看次数

ASP.net身份 - UserManager <TUser>如何访问角色?

...而我们只在Microsoft.AspNet.Identity中.(我们甚至没有查看Microsoft.AspNet.Identity.EntityFramework的基本实现.)

该UserManager班只需要在一个IUserStore<TUser>在构造函数中.它没有IUserRoleStore<TUserRole>我想象的需要访问以确定是否UserManager.IsInRoleAsync(string, string).

我认为UserStore的实现也会有一个IsInRoleAsync(string, string)功能(然后它会都有意义),但事实并非如此.

另一个奇怪的事情 - 如果UserManager在其实现中知道所有内容,那么UserManager如何才能执行密码设置和重置?我们正在处理IUser- 仅使用string Id和string UserName作为属性?

user-management account-management asp.net-identity

4
推荐指数
1
解决办法
3509
查看次数

实现"继承"(实现)通用接口的接口?

interface ITurtle
{        
    void Fight();
    void EatPizza();
}

interface ILeonardo : ITurtle
{
    void UseKatana();
}

interface IRaphael : ITurtle
{
    void UseSai();
}

interface IDonatello : ITurtle
{
    void UseBo();
}

interface IMichelangelo : ITurtle
{
    void UseNunchuku();
}
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个可以完成所有4个的大乌龟怎么办?我想编码:

class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo
{
    // Implementation not shown for brevity.
}
Run Code Online (Sandbox Code Playgroud)

这是否可能,因为现在,似乎我必须实施Fight(),EatPizza()每次4次.但我认为这两个常用功能将解决,只需要实施一次,对吧?

我可以创建4个中间接口而不继承ITurtle,然后GrandTurtle实现ITurtle.这解决了接口继承问题,但现在它在语义上看起来是错误的,因为它ITurtle看起来像第5个兄弟,它不是.另外,我希望能够创建特定于龟的类,例如class BostonLeonardo : ILeonardo.

我从许多地方读过,似乎是一场无休止的争论 - 有人说"接口内的继承"很精细,那些说不是 - 我要么不理解他们的解释,要么他们只是说这是不好的做法而不解释为什么.

c# inheritance interface interface-implementation

4
推荐指数
1
解决办法
406
查看次数

不支持接口的映射,但Linq-Sql对象已经实现了属性

因此,我在 VS 中从现有数据库创建了一个 DataContext (Linq-Sql)。它有一个名为 Users 的表,因此我有一个User对象。我特别想关注UserID和Username属性。

现在,我有一个界面:

interface IUser
{
     int Id { get; }
     string Username { get; }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个部分User类并实现IUser. 这样做的原因是,我可以在许多地方将任何 User 视为 IUser,而不必关心确切的 User 类:

public partial class User : IUser
{
    public int Id
    {
         get { return UserID; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有实现Usernameget 属性,因为我知道实体对象已经实现了它。

当我有一个查询时,dc.Users.SingleOrDefault(p => p.Id == 5);我知道这是一个错误,因为它会将该调用转换为 SQL 语句,并且它会尝试查找Id不存在的列 - UserID 存在。所以我理解这个映射问题。

当我查询时dc.Users.SingleOrDefault(p => p.Username == …

sql linq interface linq-to-sql linq-expressions

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

ASP.net OnClientClick 有一个异步调用 - 只有当异步调用完成并成功时,如何处理服务器端 OnClick?

我有

<asp:Button ID="Submit_BT" runat="server" OnClientClick="return clientClick();" OnClick="Submit_BT_Click" />
Run Code Online (Sandbox Code Playgroud)

然后在 JS 中:

function clientClick(){
     // using jQuery
     $.get(..., function(result){
         // Perform some processing on result
         // Proceed with the server-side postback
     },
     function(error){
         // Don't proceed with server-side postback
     });

     return true; // ?---
}
Run Code Online (Sandbox Code Playgroud)

我希望 $.get 在回发之前成功完成,但我认为回发永远不会发生,或者总是会发生。我可以简单地说return $.get(...); 这该怎么做?

这个问题类似,解决方法是设置async: false,部分回答了我的问题。当调用完成时,它可能是成功的,也可能是错误的。只有当调用成功时,我才想继续进行服务器端点击。如何从$.get()'s 处理程序内部“返回真或假” ?

javascript asp.net jquery

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

DotNetNuke 弹出 CSS 已禁用?

我目前可以使用 .DNN 在 DNN 中弹出一个模式窗口dnnModal.show(Url, ...);。

当我将 Url(DNN 网站内的页面)作为单独页面查看时,一切看起来都符合预期(所有框架和自定义 CSS 样式都适用)。当我在弹出窗口中查看 Url 时,所有 CSS 都消失了。

我认为 Url 可以是任何东西(甚至在域之外)这一事实可以解释为什么我们丢失了“本地”CSS。如果网址位于网站内,是否有办法“保留”弹出窗口中显示的网站外观?

作为一个旁白问题,是否有一种正确的方法可以在弹出窗口上加载 DNN 模块 - 而不是上面的 Url(因为这实际上是该模块所在的 DNN 页面的 Url)?如果可能的话,这可能会解决缺少 CSS 的问题。

dotnetnuke module popup

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

Knockout和jQuery对话框 - 当视图模型绑定上下文不是document.body时?

假设我们有一个简单的模型和视图模型:

// Model
function Model(prop) {
     this.Prop = ko.observable(prop);
}

// View Model
function ViewModel(){
     var self = this;

     this.CurrentItem = ko.observable(new Model("abc"));

     this.Edit = function(){
          $(#"EditDiv").dialog("show");
     }
}
Run Code Online (Sandbox Code Playgroud)

然后是html:

<body>
      <input type="button" data-bind="click: Edit"/>
      <div id="EditDiv" data-bind="with: CurrentItem">
         <input type="text" data-bind="value: Property" />
      </div> 
</body>
Run Code Online (Sandbox Code Playgroud)

然后是页面底部的脚本:

$(document).ready(function(){
      $("#EditDiv").dialog({ /* settings to hide it first. */ });

      ko.applyBindings(new ViewModel());
});
Run Code Online (Sandbox Code Playgroud)

上面的代码工作,并将弹出一个带有文本框"abc"的jQuery窗口.

但是,如果我在div中划分页面并为每个div分配不同的模型视图,则绑定会中断:

<body>
     <div id="ForAnotherBindingContextDiv">
     </div>
     <div id="TargetContextDiv">
          <input type="button" data-bind="click: Edit"/>
          <div id="EditDiv" data-bind="with: CurrentItem">
              <input type="text" data-bind="value: Property" …
Run Code Online (Sandbox Code Playgroud)

data-binding jquery dialog modal-dialog knockout.js

0
推荐指数
1
解决办法
5163
查看次数