我正在为我的应用程序编写URL重定向,以下是我在global.asax中添加的代码
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path.ToLower();
//string placename = string.Empty;
// Regular expressions to grab the productid and productname from the page.aspx
//Here I am using regular expression to match tghe pattern.
Regex regex = new Regex(@"displayplace/(\d+)/(.+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);
if (matches.Count > 0)
{
var referenceID = matches[0].Groups[1].ToString();
var name = matches[0].Groups[2].ToString();
incoming.RewritePath(string.Format("~/Layouts/Places/View.aspx?{0}={1}&{2}={3}", Source.Constants.Place.REFERENCE_ID, referenceID, Source.Constants.Place.NAME, name));
}
}
这是我的页面存储在文件夹中的方式
[![在此处输入图像说明] [1]] [1]
我在母版页上定义了“添加新地点”按钮,以便所有页面上都显示链接。 这是添加新按钮链接的标记
<a runat="server" href="~/layouts/places/create">Add new Place</a>
如果我在浏览器中输入places.local并单击添加新按钮,则会转到places.local / layouts / places / create(工作正常)
如果我输入(places.local / layouts / places / view?referenceID = 32&amp; name =测试地点更改),则转到(places.local / displayplace / 32 /测试地点更改),这是正常的
但是
如果我点击查看页面中的新按钮,它就不会去任何地方,只能使用此网址刷新页面 (places.local / displayplace / 32 / create)错误,它应该转到places.local / layouts / places / create
更新
为我工作,我使用了这里描述的方法 link