在Web窗体应用程序中路由URL [英] Routing URLs in Web forms application
问题描述
大家好,
如何编写简单的路由URL,如何从global.asax进行cal路由
我的global.asax代码是
protected void Application_Start( object sender,EventArgs e)
{
// 添加路线。
RegisterRoutes(RouteTable.Routes) ;
}
void RegisterRoutes(RouteCollection路线)
{
routes.MapPageRoute(
HomeRoute,
Home,
〜/ Employer /Employer_Login.aspx
);
}
如何计算Employer_login.aspx页面中的路径
请帮帮我。
你可以重写/路由实际点击页面之前的URL,为此,在global.asax中添加路由代码
protected void Application_BeginRequest(object sender ,EventArgs e)
事件。如果您已经在页面上(例如Employer_login.aspx),您可以使用
Response.Redirect(newurl)
要在Application_BeginRequest中重定向URL,您可以使用:
Context.RewritePath(new URL come here,true);
将某人发送到其他网址。
和 Request.Url 检查用户输入的URL,获取QueryString等。
这是一个简单的例子,它将.aspx添加到输入的任何URL,其中在该位置有.aspx文件但是没有它指定了URL:
(例如用户输入你的网站/默认,您希望将其更改为yourwebsite / default.aspx):
/ / 已请求的物理文件。
var requestedfile = Server。在MapPath(Request.Url.AbsolutePath);
// 文件的扩展名。
var filetype = Path.GetExtension(requestedfile).ToLower();
// 查询参数作为网址的唯一参数
var incomingparamsdir = incomingparams.Length > 0 ? ? + incomingparams: < span class =code-string>;
if ((filetype == 。 || filetype == )&& requestedfile .Length > 0 )
{
filetype = .aspx;
requestedfile = Path.ChangeExtension(requestedfile, .aspx);
if (File.Exists(requestedfile))
{
Context.RewritePath(Path.ChangeExtension(Request.Url.AbsolutePath, .aspx)+ incomingparamsdir);
return ;
}
}
希望这能解决您的问题。
Hi All,
How to write easy Routing URLs, how to cal route from global.asax
my global.asax code like is
protected void Application_Start(object sender, EventArgs e)
{
// Add Routes.
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"HomeRoute",
"Home",
"~/Employer/Employer_Login.aspx"
);
}
how to cal the Routes in Employer_login.aspx page
please help me.
You can rewrite/route the URL before it actually hits a page, to do this add your routing code in global.asax''s
protected void Application_BeginRequest(object sender, EventArgs e)
event. If you are already on the page (eg Employer_login.aspx) you can use
Response.Redirect("newurl")
To redirect a URL in Application_BeginRequest you can use:
Context.RewritePath("new URL comes here", true);
to send someone to a different URL.
and Request.Url to check the URL the user entered, get the QueryString, etc.
Here''s a simple example that will add .aspx to any URL entered where there is an .aspx file at the location but the URL was specified without it:
(eg user entered yourwebsite/default and you wish to change it to yourwebsite/default.aspx):
//The physical file that has been requested. var requestedfile = Server.MapPath(Request.Url.AbsolutePath); //The extension of the file. var filetype = Path.GetExtension(requestedfile).ToLower(); //The query parameters as the sole parameters of the url var incomingparamsdir = incomingparams.Length > 0 ? "?" + incomingparams : ""; if ((filetype == "." || filetype == "") && requestedfile.Length > 0) { filetype = ".aspx"; requestedfile = Path.ChangeExtension(requestedfile, ".aspx"); if (File.Exists(requestedfile)) { Context.RewritePath(Path.ChangeExtension(Request.Url.AbsolutePath, ".aspx") + incomingparamsdir); return; } }
Hope this solves your issue.
这篇关于在Web窗体应用程序中路由URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!