Routing in Asp.Net 4.0 - c#

I have successfully setup routing in Asp.Net 4.0 webforms. I have set up:
routeCollection.MapPageRoute("Default Page", "Default/{ProductName}/{CategoryName}", "~/Default.aspx");
However, problem is even though the user browses to default.aspx, the page still shows up. How can I avoid this? I want only the MapPageRoute to work. I want that when user browses to default.aspx some error should be thrown or 404 page should be shown etc. In short I do not the user to browse through default.aspx. How can I do this?
Thanks in advance :)

you can handle this issue through two ways.
In global.asax in Request_Start event check that if the requested url end by .aspx redirect to error page.
Use Url rewriter, by regular expression identify the wrong requests and redirect them to custom error page.

Never used routing in WebForms myself, but have you tried looking at the Request.Url? If that ends with/contains you could handle this by redirecting to an error page.

Related

site will not redirect to custome page and throw 404 error in asp.net site

I don't know this question is already asked or not. But i am stuck in one problem.
I have one CMS application in that some page are static and some are dynamic.
When i access url using http://abc.com/abc.aspx
Now in this case if abc.aspx is cms page then it will redirect me to that page but if it is not cms page then this will redirect me to the my custom page http://abc.com/page-not-found.aspx
Now my question is that if i write only http://abc.com/abc then it will didn't redirect me to the http://abc.com/page-not-found.aspx but it will throw me the error 404 page or directory not found.
Now I have to check two things
1) if any custome page is not there then it will display http://abc.com/page-not-found.aspx
2) when http://abc.com/page then it will also redirect me to http://abc.com/page-not-found.aspx
Please help me our from this. This will work fine in my local but only problem in live environment.
Thanks in advance.
Regards
AB Vyas
You may need to look into Handlers in your web.config .
http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx

Response.Redirect() is appending the body of HTML to the URL

I am using Response.Redirect in ASP .NET C# application, to redirect to a different web page based on success or failure.
But Response.Redirect is not working. Instead or redirecting to the new link, the body or the contents of the target web page is appended to the existing URL. I tried it for a simple HelloWorld page and still its not working. For ex:
If I am in home page: http://www.example.com/test/default.aspx
and if I want to redirect to HelloWord html page, then the final URL would be
Response.Redirect("~/../hello.html"); but I get http://www.example.com/<p>Hello%20World!!</p>
Due to the improper URL, I am getting "Access Denied Error".
Thanks for the help in advance.
You should use:
Response.Redirect("~/hello.html");
if your hello.html is in the application root directory.
Or if you want a relative parent directory to the current page:
Response.Redirect("../hello.html");
~ references the application root directory, so with "~/.." you are trying to access a parent of the root directory, that is not allowed.
I think you should use Server.Transfer() instead. It will redirect you to a new page. For example:
Server.Transfer("Home.html")
if the page is in the same directory, otherwise just use relative link
You cannot redirect to a file that is outside of an IIS site, as "one level up from the app-root" probably is.
Add the following code to the Page_Load event:
Response.Redirect("http://www.microsoft.com");

jQuery Mobile showing incorrect URL

On a MVC3 project I'm working on whenever I do a redirect to action on the controller the URL that is shown in the browser is the original request. Not the redirected one. Has anyone seen this or know of a workaround for it?
So for example. I can click a link with a href that equals...
http://www.test.com/account/LogOut
Which on the controller will redirect to the homepage, but once I'm logged out and the homepage is shown...the url still shows http://www.test.com/account/LogOut
This is causing some quirky behavior with a couple of other things that I've got going on.
You should use the attribute 'data-ajax="false"' in your logout link.

Why is my response.redirect() not working properly?

Two aspx pages are involved with the problem. in one form I am collecting the entity and binding it in a session variable and then with button clicked, I am trying to get to the other aspx page with response.redirect("") method. But, the problem is its gives me an error message with a strange URl.
let me show you the code I have writen
formSaleMoneyReceiptEntity = ViewFormSaleMoneyReceipt_DAO.GetMoneyReceiptByFormSL(formSl);
Session["MoneyReceipt"] = formSaleMoneyReceiptEntity;
Response.Redirect("~/Reports/MoneyRepeiptFormReport.aspx",false);
I am using local host and the URl I am getting is that
http://www.google-feed.net/results.php?q=localhost 5808 StudentManagement FormSaleMoneyReceipt aspx &cx=002904446094441487865%3Ate-nlsbrcdy&cof=FORID%3A10&ie=UTF-8&said=&do=search&empty=0&from=2&CID=1
why is this so? I don't have any idea. Please help me out.
It looks like the URL that is passed to the browser doesn't exist, so you're getting a redirect to a Google search page instead.
Is it ~/Reports/MoneyReceiptFormReport.aspx by any chance?
A good tool to use to debug this kind of situation is Fiddler. This sits as a proxy between your Web server and your browser and issues a trace of what requests and responses are made. Browsers have a habit of reformatting error messages, Fiddler will show you exactly what's sent to the server and what comes back.

Transfer instead of redirect to login in forms authentication

I give to google a sitemap with all my pages, when the crawler tries to access them he gets redirected to the login page.
In the login page I write an explanation of what the page does so the crawler can see that each page is different.
The problem now is that the bot is clever enough as to recognize that it is a redirect:
URLs not followed
When we tested a sample of URLs from your Sitemap, we found that some URLs redirect to other locations. We recommend that your Sitemap contain URLs that point to the final destination (the redirect target) instead of redirecting to another URL.
HTTP Error: 302
If instead of using a redirect in RedirectToLoginPage(String), Microsoft had used a Server.Transfer, google would never find out it is actually the same page.
Any Ideas?
As Carl said, if access to your content requires a log in, then there isn't a whole lot you can do.
However, if you can separate out a "teaser" of each content page and have a link to "read more" from those pages that requires a login, then you'll be good to go.
The teaser page should have enough searchable text that google will be able to successfully include it in search results.
There are a number of sites that do just this. You search for something, click on the link to go to their site. Once there you can see maybe two paragraphs worth of information. If you want more a link takes you to a login / register page.
Okey I found a not elegant solution, but it suits my needs:
http://forums.asp.net/t/1358997.aspx
""For now, I found a workaround: I capture the End-request event and see if the status code is "302 redirected"; if it is, I'll just alter the address from there and do whatever I need to do. Not the most elegant solution (and requires more processing for every page request; not just login redirects), but at least it works.""

Categories