- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在研究 ASPNET MVC 4 和 WebApi。移动设备将使用 webapi 方法。我们需要保护服务,我们正在使用的是以某种特定方式加密数据。
现在,我需要在到达 Controller 之前解密调用。如果解密的信息有效,它应该像往常一样继续到 Controller ,如果不是,我会将用户路由到一些错误方法。
要实现这一点,我认为最好的办法是自定义 HttpHandler 和自定义 RouteHandler。我正在学习教程 here
public class MvcSecurityRouteHandler:IRouteHandler
{
public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcSecurityHttpHandler(requestContext);
}
}
public class MvcSecurityHttpHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState, IRouteHandler
{
public RequestContext RequestContext { get; set; }
public MvcSecurityHttpHandler(RequestContext requestContext)
{
this.RequestContext = requestContext;
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext httpContext)
{
var controllerId = RequestContext.RouteData.GetRequiredString("controllerId");
IController controller = null;
IControllerFactory factory = null;
try
{
factory = ControllerBuilder.Current.GetControllerFactory();
controller = factory.CreateController(RequestContext, controllerId);
if (controller != null)
{
controller.Execute(RequestContext);
}
}
finally
{
factory.ReleaseController(controller);
}
//string originalPath = httpContext.Request.Path;
//HttpContext.Current.RewritePath(httpContext.Request.ApplicationPath, false);
//IHttpHandler httpHandler = new MvcHttpHandler();
//httpHandler.ProcessRequest(HttpContext.Current);
//HttpContext.Current.RewritePath(originalPath, false);
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
throw new NotImplementedException();
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
var defaults = new RouteValueDictionary
{{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}};
var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler());
routes.Add(customRoute);
routes.MapRoute(
name: "DefaultWebApi",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
}
}
全局.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
var defaults = new RouteValueDictionary
{{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}};
var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler());
routes.Add(customRoute);
}
在 Application_Start 中
RegisterRoutes(RouteTable.Routes);
服务启动后,我在 ProcessRequest 中创建了一个断点,它没有被命中。可能缺少什么?这是正确的做法吗?
最佳答案
如果您还没有,您需要先在 global.asax 或您的 web.config 文件中注册处理程序。
http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx
关于c# - 使用 ASP NET MVC 4 和 webapi 的自定义 http 处理程序和路由处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593114/
我刚刚用 java-swing 为我写了一个桌面时钟,我希望每次登录时该时钟都运行。 为此,我将我的 jar 文件添加到 start 文件夹,我让时钟开始运行。 但我的问题是 - 任务栏中显示的图标允
我正在尝试编写一个程序来检查用户是否上传了新视频。我想让它成为一项后端工作,不断检查用户最近的视频,然后使用我的应用程序向我的用户发送推送。有关于这个问题的任何文档或示例代码吗?我完全不知道从哪里开始
我正在为我的 Raspberry Pi 编写一个程序,该程序由两个主要部分组成: 使用 Spotify-API“Libspotify”搜索音乐并播放音乐的 C 程序。 一个在 apache2 We
我做了一个C++生成命令行并将命令转发给它的程序。目前,我正在将 cmd 控制台的输出发送到一个文件,并在我的 C++ 程序中读取它。但我想让它与管道一起工作。 是否可以从 Windows cmd 行
是否可以使用 C 程序和 malloc 找出处理器的页面大小?而不是使用 sysconf() 调用? 最佳答案 如果你可以#include一些linux内核头文件,你可以在中找到宏PAGE_SIZE
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想实现一个算法: 从 Ruby on Rails 应用程序获取数据库对象作为输入, 对输入执行计算, 根据计算查询 Rails 数据库, 并根据查询生成一组有序结果。 我将用 C(也许是 Objec
我今天的任务是创建一个 Python 脚本(例如 A.py),它可以执行以下操作: 启动 C 程序(例如 CProg)并传递一些参数 启动另一个 Python 脚本(例如 B.py)并传递其他参数 加
我有一个在后台运行的 .NET 程序,需要创建一个可以与该程序通信的 Windows Shell 扩展。起初我以为我会在 .NET 中创建我的 Shell Extension,并使用 IpcServe
Python 程序做前端工作,C 程序做后端工作。它们中的每一个都是一个单独的过程。 Python 进程充当主进程,向 C 进程发送信号和事件。 C 进程生成统计信息、计数器和信息,这些信息被发送回
如何与 shell 脚本共享 C 头文件? shell 脚本通过命名管道与 C 程序通信。让我们假设 C 头文件中定义的枚举 SAMPLE_ONE 由 C 程序写入管道。 shell 脚本从管道中读出
我有一些客户/候选人提示我的程序不能在他们的 Windows 7 64 位版本上运行(已通过屏幕截图确认)。错误很奇怪,例如: in the trial version i am getting a
这个问题在这里已经有了答案: Why SDL defines main macro? (2 个答案) 关闭 7 年前。 我在 Windows 操作系统下使用 QT Creator 的简单程序中使用
我的导师给了我们一个基本的 C shell 来扩展,我目前正在努力让 shell 在用户在命令行中输入“cd [directory]”时更改目录。我已经得到它来停止段错误,但它不会更改目录。谁能告诉我
我以前有过这个工作,但我使用的是指针。 getenv() 不断崩溃,所以我使用 sprintf() 复制了结果。现在我想用 : 删除并只打印第一次出现的地方。请帮忙! #include #inclu
你好,我第一次使用 C primer plus book 学习 C,然后在第 16 章关于 C11 标准的 _Generic 我在 Eclipse c/c++ 中编写了一个程序并构建它产生了 8 个错
我正在尝试从另一个 C 程序执行 python 程序,其中 py 脚本的返回值为 int array[3] 我可以从 python 退出代码中获取这个数组吗?? 编辑:如果问题不清楚,我可以将 pyt
// The countChicken() method should count the number of occurrences of the word chicken (or some oth
我已经通过 ZMQ 使用同一类成功地从 C# 和 C++ 程序传输数据,其中 C++ 类是数据定义,编译器幸运地将字节数组屏蔽到类。 我如何在 C# 和 Node.js 程序之间做同样的事情?我认为他
任何人都可以为我指明有关 makefile 如何工作以及如何使用 eclipse 从头开始基本程序的好教程的方向吗?我正在为 fedora 和 C++ 使用 eclipse 3.4.1 版。提前致
我是一名优秀的程序员,十分优秀!