I am trying to add page routing (I use regular asp.net 4.0, not mvc), so that when a user goes to:
http://sitename.com/public/member/view/andrey
they would get to:
http://sitename.com/public/memberprofile.aspx?userName=andrey
I added following in Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/View/Member/{username}",
"~/Public/MemberProfile.aspx");
}
But when I try going to http://sitename.com/public/member/view/andrey in my browser, I get 404
Is there anything else that needs to be done for this routing to work other than adding a page route map?
Thanks!
Your route says Public/View/Member/{username}
But your link is /public/member/view/andrey
This would definitely 404
Why not try and change your route to
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/Member/View/{username}",
"~/Public/MemberProfile.aspx");
}
and see what happens
I actually found this great article that helped me fix my problem: http://blogs.msdn.com/b/rjacobs/archive/2010/06/30/system-web-routing-routetable-not-working-with-iis.aspx
Related
I want to redirect url to my site.
I mean I have this domain.
www.samplesite.com
and I have this site : www.samplesite.com/test/index.aspx
but I want to enter www.samplesite.com and goes to my site.
How can I do that?
I use dot net ,4.5 framework.
When using asp.net web forms you can create a Default.aspx page, in the Page_Load method in the Default.aspx.cs file call this:
private void Page_Load(object sender, EventArgs e)
{
Response.Redirect("~/test/index.html");
}
can anyone please provide me the details how we can implement custom URL rewriting in asp.net
My current url is look like below :
www.domainname.com/News/default.aspx?newstitle=todays latest news
And now I would like to redirect to below url :
www.domainname.com/News/todays-latest-news
Please suggest me how we can achieve the same.
Add this to global.asax
using System.Web.Routing; //top of the page
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "news/{news}", "~/news/default.aspx");
}
And then you can get the news title in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
if (this.RouteData.Values.Count > 0)
{
string newstitle = this.RouteData.Values[0].ToString();
}
}
to achieve your task use the concept called asp.net routing ,
here is the few examples refer for better understanding
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
You can use URLRewriter.Net for this purpose. It's very easy to integrate into asp.net project and also it's open source . Add the dll file of urlRewriter.Net into your project and set the rewriting rule in your web.config file. Although be careful when using it with ajax postback pages. In Raw url if you get ajax postback problem .
so this is the scenario:
I have a base class for all login-controlled pages:
public class SessionControlledPage : Page
{
protected virtual void Page_Load(Object sender, EventArgs e)
{
Response.AddHeader("Refresh", Convert.ToString(Session.Timeout * 60 + 5));
if (Session.IsNewSession)
{
Response.Redirect("~/login.aspx");
}
}
}
And a regularpage.aspx page that inherints from the base class:
public partial class RegularPage : SessionControlledPage
{
override protected void Page_Load(Object sender, EventArgs e)
{
base.Page_Load(sender, e);
Server.Transfer("~/otherpage.aspx");
}
}
Now let's say the original URL was http://localhost/regularpage.aspx and that Session.Timeout = 5. The problem appears when the refresh happens after the transfer takes place. I'm getting a 404 Not Found error and the URL changes for http://localhost/305. Notice that 5 * 60 + 5 = 305.
Do you know why is this happening? got a solution?
PD: I use transfer because I need to access some properties of regularpage.aspx on otherpage.aspx, maybe you could provide another solution for this too.
There is very little reason to call base.Page_Load if all you're going to do is then call Server.Transfer. What exactly are you trying to accomplish? If you're just accessing some properties you need to abstract this into some business logic that does not rely on the Page object.
This is also what is causing the 404 issue... for this to happen, the problem has to be in the rendered output of the page (check it out). It seems like you are cutting off one page right after the meta-refresh tag and then starting a new page and ASP.NET is just dumping it all into the same response stream. In short, you're doing it wrong. :) You might be able to fix this with a well-placed Response.Clear(), but that's not the real problem here... and you'd lose your refresh tag.
I have a page, Default.aspx, and a UserControl, HelloControl.ascx. In the page, I dynamically instantiate the control as follows:
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
c.Greet();
}
This works fine, and the user control writes "Hello from a control" to the response. I have no #Register directive in Default.aspx, but when I try a similar dynamic control creation on a client's machine, I get an error that the "Type or Namespace does not exist".
I have even gotten feedback, on forums, from MS, that I need the #Register directive, but I obviously don't. Can anyone help me out with info on how and when the user control is compiled if no #Register directive references it?
EDIT: I have tried a different direction of investigation, losing my initial call to LoadControl, and I still can't reproduce the problem. The following code also works fine on my dev machine, without any #Register directive.
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = new HelloControl();
Response.Write(c.Greet());
}
use full path to control :
this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));
Normally I create web application projects and use code-behind, but I have a requirement to create an small throwaway demo app using code-inline.
I added a global.asax file to the app, but for some odd reason, Visual Studio 2008 SP1 won't let me edit any of the code between the script tags i.e. add code to the event handlers such as Application_Start, Session_Start. VS does however let me edit outside the script tags.
This is just a simple file based web app using the built in web server.
Any ideas what's going on?
This is the code-inline global.asax VS creates:
<%# Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when
// the sessionstate mode
// is set to InProc in the Web.config file.
// If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Ok, so here is the answer:
You can see part of the explanation here and here.
Basically the global.asax file doesn't actually get compiled, so VS2008 put in a fix to prevent you from modifying it, since your modifications will have no effect.
I don't think VS allows inline script in Global.asax. You need to put the code in Global.asax.cs instead.
The only time I ever saw this kind of of thing is when I am in debug mode, and I didn't realize it. Seems pretty basic, but have you checked that?
tried right-clicking -> view code?