我想用自己的方法替换.NET或ASP MVC框架中包含的扩展方法.
例
public static string TextBox(this HtmlHelper htmlHelper, string name)
{
...
}
Run Code Online (Sandbox Code Playgroud)
可能吗?我无法使用override或new关键字.
我刚刚开始使用VS 2012 RC,我正在创建一个ASP.NET MVC 4 Web应用程序,我计划在其中提供基于HTML的用户界面和基于WebApi的编程接口.
对于我的HTML网站,我有一个控制器和每个模型的视图(MVC!),并且路由"按惯例"工作,例如,URL /client连接到我的ClientController.我的ClientController来源于Controller.
对于我的API,我将创建源自的新控制器ApiController.我自然希望我的API网址与我的HTML网址类似,因此我希望客户端信息可用于/api/client.但是,使用按惯例路由,这表明我需要一个命名的ApiController ClientController.我已经ClientController上课了.
我该如何处理?我需要自定义路由吗?我是否将API类放在不同的命名空间中,以便我可以为它们指定相同的名称?
更新:这个问题似乎表明我的API控制器的不同命名空间就是我所需要的:混合web api控制器和站点控制器
在ASP.NET 5应用程序中,我配置了MVC和Identity框架,如下所示:
app.UseMvc(config=>{
config.MapRoute("Default", "{controller}/{action}/{id?}", new
{
controller = "Home",
action = "Index"
});
});
Run Code Online (Sandbox Code Playgroud)
并添加身份服务:
services.AddAuthentication();
services.AddAuthorization();
services.AddIdentity<CrmUser, CrmUserRole>(config => {
config.User.RequireUniqueEmail = true;
})
.AddUserStore<MongoUserStore>()
.AddRoleStore<MongoUserStore>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
和
app.UseIdentity()
.UseCookieAuthentication(i => { i.LoginPath = "/Account/Login";});
Run Code Online (Sandbox Code Playgroud)
该示例定义如下:
public class MyApiController : Microsoft.AspNet.Mvc.Controller
{
[Authorize]
public async Task<ActionResult> Foo()
{
return Ok();
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我也有一些控制器,我想以API方式使用.在ASP.NET 5中,它们都具有相同的基类,因此API和视图控制器之间没有区别.
因此,当调用需要授权的未经授权的api时,我会得到一个HTTP 200和登录页面而不是HTTP 401.
在Shawn Wildermuth的博客文章中,我发现了这一点
services.AddCookieAuthentication(config =>
{
config.LoginPath = "/Auth/Login";
config.Events = new CookieAuthenticationEvents()
{ …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc asp.net-identity asp.net-core-mvc asp.net-core