我正在尝试使用基本控制器创建基本的REST api,如下所示:
基类:
public abstract class WebApiEntityController<TEntity> : ApiController
where TEntity : EntityBase<TEntity, int>
{
private readonly IRepository<TEntity> _repository;
protected WebApiEntityController(IRepository<TEntity> repository)
{
_repository = repository;
}
[Route("")]
[WebApiUnitOfWork]
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, _repository.ToList());
}
[..........]
Run Code Online (Sandbox Code Playgroud)
派生类:
[RoutePrefix("api/TimesheetTask")]
public class TimesheetTaskController : WebApiEntityController<TimesheetTask>
{
private readonly IRepository<TimesheetTask> _timeSheetTaskRepository;
public TimesheetTaskController(IRepository<TimesheetTask> timeSheetTaskRepository) : base(timeSheetTaskRepository)
{
_timeSheetTaskRepository = timeSheetTaskRepository;
}
}
Run Code Online (Sandbox Code Playgroud)
但是在路由〜/ api/TimesheetTask /上调用GET导致找不到404.
根据这个答案,属性路由不能被继承.所以我的问题是,如何为所有域模型编写一致的API而无需复制和粘贴代码?
我知道我可以使用这种配置进行常规路由:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional } …Run Code Online (Sandbox Code Playgroud) 首先,我想说这只是一个学习练习,我不打算在生产中使用它.
我在Golang中编写了一个带有两个函数的小应用程序:encrypt(plaintext string, password string)和decrypt(encrypted string, password string)
加密步骤是:
返回的字节数组如下所示:
[256 bit salt] [128 bit iv] encrypted([256 bit hmac] [plaintext])
Run Code Online (Sandbox Code Playgroud)
解密时:
我没有疯狂到在任何生产项目中使用我自己的加密脚本,所以请指向我这样做的任何库(简单的密码/消息加密相对安全)
这是两个函数的源代码:
package main
import (
"io"
"crypto/rand"
"crypto/cipher"
"crypto/aes"
"crypto/sha256"
"crypto/hmac"
"golang.org/x/crypto/pbkdf2"
)
const saltlen = 32
const keylen = 32
const iterations = 100002
// returns ciphertext of the following format:
// [32 bit salt][128 bit iv][encrypted plaintext]
func encrypt(plaintext string, password string) …Run Code Online (Sandbox Code Playgroud)