如何从Global asax

时间:2015-06-11 10:30:01

标签: c# routing asp.net-mvc-5

我正在尝试准备一个301重定向,因为我犯了'收到错误'

我正在努力寻找一种从动作和控制器名称中获取URL的方法。

我知道UrlHelper.Action但它在Global.asax中不存在。如何获得对此方法的访问权限?:

// Add permanent redirection for retired pages (Application_BeginRequest())
if (HttpContext.Current.Request.Url.LocalPath.ToLower().StartsWith("/blah/listrecieved"))    
{
    HttpContext.Current.Response.RedirectPermanent(/*Need url generated from action and controller*/);
}

或者我已经创建了一个路线,如果我应该如何获得该字符串,这也没关系,但我不确定如何:

routes.MapRoute(
    name: "blah-list-received",
    url: "blah/list-received",
    defaults: new { controller = "Blah", action = "ListReceived" }
);
例如,它可能如下所示:

// Add permanent redirection for retired pages
if (HttpContext.Current.Request.Url.LocalPath.ToLower().StartsWith("/blah/listrecieved"))    
{
    HttpContext.Current.Response.RedirectPermanent(routes.GetUrl( "blah-list-received" ) );
}

1 个答案:

答案 0 :(得分:3)

您需要自己构建var url = new UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes) .Action("YourAction", "YourController", new { paramName = paramValue });

function grab_ans_given_ques($conn, $question, $examName){

    // Split the string here into two parts and join with a wildcard (%)
    $ques = substr($question, 0, 20)."%".substr($question, -20, strlen($question));
    $sql  = $conn->prepare("SELECT `AText` FROM `Exam_QA_Data` WHERE `Name` = '$examName' AND `QText` LIKE '%$ques%'");
    $sql->execute();

请参阅MSDN