本文介绍了我如何获得引荐乌里控制器和动作的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有大量的信息从控制器和动作名称建立尤里斯,但我怎么能围绕这样做的其他方式?

There's a lot of information for building Uris from Controller and Action names, but how can I do this the other way around?

基本上,所有我想要实现的是获得引用页(即Request.UrlReferrer)控制器和动作名称。有没有一种简单的方法来实现这一目标?

Basically, all I'm trying to achieve is to get the Controller and Action names from the referring page (i.e. Request.UrlReferrer). Is there an easy way to achieve this?

推荐答案

我想这应该做的伎俩:

// Split the url to url + query string
var fullUrl = Request.UrlReferrer.ToString();
var questionMarkIndex = fullUrl.IndexOf('?');
string queryString = null;
string url = fullUrl;
if (questionMarkIndex != -1) // There is a QueryString
{    
    url = fullUrl.Substring(0, questionMarkIndex); 
    queryString = fullUrl.Substring(questionMarkIndex + 1);
}   

// Arranges
var request = new HttpRequest(null, url, queryString);
var response = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(request, response)

var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));

// Extract the data    
var values = routeData.Values;
var controllerName = values["controller"];
var actionName = values["action"];
var areaName = values["area"];

我的Visual Studio是目前下跌了,所以我无法测试它,但它应该工作正常。

My Visual Studio is currently down so I could not test it, but it should work as expected.

这篇关于我如何获得引荐乌里控制器和动作的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:23