在ASP.Net MVC路由之前数据库选择 [英] Database selection before routing in ASP.Net MVC

查看:59
本文介绍了在ASP.Net MVC路由之前数据库选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在asp.net mvc的4,一种从web.config中选择数据库设置任何路由发生之前,像pre路由钩子codeigniter(PHP)。是什么让这些设置保存会话或上下文对象的最佳方法? P.S我是新来asp.net的MVC。

I'm looking for a way in asp.net mvc 4, to select database settings from web.config before any routing takes place, like pre routing hooks in codeigniter(php). What is the best approach to keep save those settings sessions or context object? P.S I'm new to asp.net mvc.

这样的php code的东西在这里提到:<一href=\"http://stackoverflow.com/questions/24879539/running-a-database-query-before-every-route-runs\">Running每条路由前一个数据库查询运行

Something like this php code mentioned here: Running a database query before every route runs

推荐答案

您要求从Web.Config中,或从数据库中获取价值?
这是一个有点混乱TBH,但这里是一些code,将两者都做。

Are you asking for getting values from Web.Config or from Database? It's a bit confusing tbh, but here is some code that will do both.

您可以创建自己的路由处理程序,在这里你可以做你想做什么都pretty得多。
然后在RouteConfig.cs,一定要使用自己的路由处理。

You can create your own Route Handler, and here you can do what ever you want pretty much. Then in the RouteConfig.cs, make sure to use your own Route Handler.

这是MyRouteHander.cs

This is MyRouteHander.cs

using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace My.Services
{
    public class MyRouteHander : IRouteHandler
    {
         ApplicationDbContext Db = new ApplicationDbContext();
         public IHttpHandler GetHttpHandler(RequestContext requestContext)
         {

             // Get route data values
             var routeData = requestContext.RouteData;
             var action = routeData.GetRequiredString("action");
             var controller = routeData.GetRequiredString("controller");

             // Get webconfig settings
             var webConfigSetting = ConfigurationManager.AppSettings["SOME_FANCY_SETTING"]
             if (!string.IsNullOrEmpty(webConfigSetting))
             {
                requestContext.RouteData.Values["action"] = webConfigSetting;
                return new MvcHandler(requestContext);
             }

             // If we have SomeDataBaseTable hit we do something else.
             if (Db.SomeDataBaseTable.Any(x => x.Action == action))
             {
                 // Lets do something with the requestContext.
                 string actionName = "SpecialAction";
                 requestContext.RouteData.Values["action"] = actionName;
                 requestContext.RouteData.Values["controller"] = "SpecialController";
                 requestContext.RouteData.Values.Add("id", Db.SomeDataBaseTable.FirstOrDefault(x => x.Action == action).Id);
             }
             return new MvcHandler(requestContext);
         }
     }
 }

App_Start / RouteConfig.cs更新routes.MapRoute(),所以它使用您的MyRouteHander。

App_Start/RouteConfig.cs update the routes.MapRoute() so it uses your MyRouteHander.

routes.MapRoute(
      "Home",
      "Home/{action}/{id}",
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "MyProject.Controllers" }
    ).RouteHandler = new MyRouteHander();

    routes.MapRoute(
      "Default",
      "{controller}/{action}/{id}",
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "MyProject.Controllers" }
    ).RouteHandler = new MyRouteHander();
...

希望这有助于!

这篇关于在ASP.Net MVC路由之前数据库选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆