我使用Moq來確定中HttpContextBase
的用法。它們是:
- 請求
- AppRelativeCurrentExecutionFilePath
- PATHINFO
Request.AppRelativeCurrentExecutionFilePath
應該返回路徑與~
,我究竟需要,所以實用類可能是這樣的一種:
public static class RouteUtils
{
public static RouteData GetRouteDataByUrl(string url)
{
return RouteTable.Routes.GetRouteData(new RewritedHttpContextBase(url));
}
private class RewritedHttpContextBase : HttpContextBase
{
private readonly HttpRequestBase mockHttpRequestBase;
public RewritedHttpContextBase(string appRelativeUrl)
{
this.mockHttpRequestBase = new MockHttpRequestBase(appRelativeUrl);
}
public override HttpRequestBase Request
{
get
{
return mockHttpRequestBase;
}
}
private class MockHttpRequestBase : HttpRequestBase
{
private readonly string appRelativeUrl;
public MockHttpRequestBase(string appRelativeUrl)
{
this.appRelativeUrl = appRelativeUrl;
}
public override string AppRelativeCurrentExecutionFilePath
{
get { return appRelativeUrl; }
}
public override string PathInfo
{
get { return ""; }
}
}
}
}
如果傳遞〜/錯誤/ NOTFOUND,例如,RouteUtils.GetRouteByUrl("~/Error/NotFound")
回報像
RouteData.Values
{
controller = "Error",
action = "NotFound"
}
也許這將是任何人的幫助。
https://stackoverflow.com/questions/8830052/how-do-i-get-the-controller-and -action-names-from-the-referrer-uri – 2017-07-26 21:02:34