0

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.

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

1
  • What do you mean by "any routing"? Are talking about accessing a page or the website for the first time during a session? If you want to do something at the start of the session, try using the Session_Start method in Global.asax. Commented Oct 13, 2014 at 6:45

1 Answer 1

4

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.

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.

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 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();
...

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Do not forget to Dispose your ApplicationDbContext ;). Use a using block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.