mercredi 18 novembre 2015

how to redirect to other page if mvcRouting failes

this is my RouteConfig.cs and i am using isValidAppId class to match the appid which comes in url to the 'modelApplicationId' which i have stored in web.config

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
namespace SCS.Web
public class RouteConfig
    public static void RegisterRoutes(RouteCollection routes)

           new { controller = "Account", action = "SignIn", id = UrlParameter.Optional },
           new {

               isValidAppId = new isValidAppId() 



public class isValidAppId : IRouteConstraint
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        var isValid = false;
        if (values["appId"] != null && WebConfigurationManager.AppSettings["ModelApplicationId"] != null)
            if (values["appId"].ToString() == WebConfigurationManager.AppSettings["ModelApplicationId"].ToString())
                return isValid = true;

        // return true if this is a valid AppId
        return isValid;


if the isValidAppId returns false i want to redirect to other Error.cshtml page.

Aucun commentaire:

Enregistrer un commentaire