Yes you can do this using "application_beginrequest" event of Global.asax
and then using the Context.RewritePath. Follow the following link for more help. The link is not for exactly what you want but will surely guide you how to accomplish your task.
WebMatrix tries to match url segments to files on disk. It's looking for root/p1.cshtml. Since it can't find it, it will look for
root/p1/index.cshtml or root/p1/default.cshtml. Neither exist. File Not Found. index/p1 will make your parameter accessible.
Or rather, it won't if you are simply using Response.Redirect("~/index.cshtml"). You need to add the parameter to the url you are redirecting to.
CyberKral
Member
2 Points
13 Posts
Route all requests to a single page
Jul 27, 2010 09:55 PM|LINK
Is it possible to route all the request to the same page without displaying the name of the page in the url.
For exemple, how can I route
www.mysite.com/2010/07/27 and www.mysite.com/user/user1
to index.cshtml
Thanks.
iGulfam
Contributor
4794 Points
947 Posts
Re: Route all requests to a single page
Jul 28, 2010 05:33 AM|LINK
Yes you can do this using "application_beginrequest" event of Global.asax and then using the Context.RewritePath. Follow the following link for more help. The link is not for exactly what you want but will surely guide you how to accomplish your task.
http://issuesdotnet.blogspot.com/2010/07/url-rewriting-making-user-friendly-urls.html
My BLOG
CyberKral
Member
2 Points
13 Posts
Re: Route all requests to a single page
Jul 28, 2010 07:20 AM|LINK
I already do that, but what I want is to do it in the razor syntax, in a cshtml file.
Can you give an exemple?
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Route all requests to a single page
Jul 28, 2010 07:29 AM|LINK
I´m not sure this will work, but try to create a _init.cshtml file, where you render index.cshtml.
_init.cshtml is loaded before all pages.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
tanatrajan
Participant
1784 Points
370 Posts
Re: Route all requests to a single page
Jul 28, 2010 02:28 PM|LINK
create _init.cshtml in the root folder of your website and put the following content. setting LayoutPage and PageData are optional as you wish.
@{
LayoutPage = "~/shared/Layout.cshtml";
PageData["Title"] = "Index";
if (!Request.Url.ToString().Contains("index.cshtml")) {
Response.Redirect("~/index.cshtml");
}
}
webmatrix init page
CyberKral
Member
2 Points
13 Posts
Re: Route all requests to a single page
Jul 28, 2010 10:27 PM|LINK
This does not work
When I request http://localhost:52182/p1 , the content of _init is not executed, I obtain HTTP Error 404.0 Not found
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Route all requests to a single page
Jul 28, 2010 11:20 PM|LINK
Do you have a file with the name "p1.cshtml"?
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
tanatrajan
Participant
1784 Points
370 Posts
Re: Route all requests to a single page
Jul 29, 2010 05:48 AM|LINK
it smells to me that p1 is a folder again. try adding a _init.cshtml in that folder as well and put the same content in my last post.
CyberKral
Member
2 Points
13 Posts
Re: Route all requests to a single page
Jul 29, 2010 07:33 AM|LINK
No p1 is not a file nor a folder, it is a arbitrary parameter.
I want to obtain it with var = UrlData[0].ToString(); // = "p1"
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Route all requests to a single page
Jul 29, 2010 09:44 PM|LINK
WebMatrix tries to match url segments to files on disk. It's looking for root/p1.cshtml. Since it can't find it, it will look for root/p1/index.cshtml or root/p1/default.cshtml. Neither exist. File Not Found. index/p1 will make your parameter accessible.
Or rather, it won't if you are simply using Response.Redirect("~/index.cshtml"). You need to add the parameter to the url you are redirecting to.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter