Referencing other ASP.NET pages symbolically, with compile-time checks? - asp.net

I'm noticing code accumulating in my project that looks like this:
Response.Redirect("/Foo/Bar.aspx");
This seems brittle -- if I move or rename Bar.aspx file, I need to find places where I've referenced it and correct those string constants, both in markup and codebehind. It seems like their should be a better way. Something like:
Response.Redirect( MyNamespace.BarPage.GetUrl() );
In other words, let the 'stack' figure out the URL I need. Note: I know that I can consolidate references to a particular page with a hand-coded BarPage.GetUrl() method, but even that seems failure-prone.
Isn't there a better way?

The best way would be to resource them. Add a meaningful key and the URL value to the resource file, and redirect that way.
Response.Redirect(Properties.ASPXUrls.FooBar);

The problem you'll face is that there's no real inherent link between a code-behind and it's code-infront except the <%#Page %> directive. There's no real reason a codebehind has to even have the same class name as the code-infront's file name, it only happens because it's convention and it's how the auto-generator lays it out.
This means you're not going to find anything you can reference at compile-time that even knows what aspx the .cs links to. The closest thing you'll find is the typeof(MyNamespace.BarPage).FullName which will give you the code-behind's name and by assuming things follow convention you could (but I don't recommend) construct the URL for the code infront page it's associated with.
Personally I think you're better off just doing a find-all for "barPage.aspx" when you rename it and doing a little refactoring. You'll have to deal with hyperlinks in the code-infront anyway. If barPage.aspx represents some abstract concept (Like "The login page") it may help to add a property for it, but if barpage is just another page with no real globally inherent meaning I'd leave it as-is.

I'd recommend creating a static class with different properties for each of the links. That way, you only have one place to update.

Redirects in general are fragile, no matter how you get the name of the next page. They are also a performance problem.
If you find them collecting in your system, the first question you should really ask is why: excessive redirects are almost always a sign of an architectural problem.

Related

Calling methods from one ASP.Net web form from another web form

A site I'm working on has two different pages, and each one is attempting to do the same thing. One of the pages, though still in use, has not been updated and so the code is no longer doing everything it is meant to do.
Rather than continue this wet solution and copy the missing code, I would like to make use of the code that has been updated. Ideally I would copy the function into a helper class or make a base class to provide this functionality, but the one function calls many private functions, and each of the private function mixes business logic with the presentation logic.
I have no documentation and a short time scale, so to do this would not be feasible. So my question is, what are the disadvantages of calling the function on page1.aspx from page2.aspx? It is a shared function so I'm hoping it will be OK but advice would be appreciated.
Sounds like a bloody mess to me. You are not to descript but without really seeing whats going on it is hard to say. If this code is touching dom elements and hitting page cycle events it will probably be a pain. I would say.. take the actual business logic and put it in another class outside of the page and then reference the business logic that way but for dom elements directly webforms is already over complicated I would try staying away from making it worse. Perhaps you can pull these dom elements into a user control and reuse it on both pages? Either way sounds like you have plenty of fun a head of you :)
It would be best if you showed some of the code, but I have to say that if the two methods are Shared, then you're probably ok.
However, such Shared code doesn't belong on either page. It belongs in some other class that can be accessed by both pages.

How to execute page methods when using custom route handlers?

When the path refers to the actual folder structure and points to the page it's not a problem, i.e. "/Default.aspx/MyMethod", however if "/" brings up "Default.aspx", then "/MyMethod" means something different. Is it even possible at all?
A possible solution, and probably a better one, is to use a web service, which is what I'm using at the moment.
You can add the following:
PageMethods.set_path('/yourpage.aspx');
I found this solution here

Problems with Server-side Includes

I desperately want to use server-side includes in a project I'm working on because I just have some HTML that repeats and I need to get it on several pages. Must I use ascx or some other include technology... I mean, will lightning strike if I use server-side includes?
My client -- the middle-person -- says "do what's easiest, this will probably be redone in a CMS soon anyway." Can I not use server-side includes?
It's ASP.NET 2.0.
Note: I feel this has been asked before, but I couldn't find it. If it has, please let me know and I will personally delete it, thanks!
Edit: Any way to get an include ON ONE LINE would be fine with me, if you have suggestions.
Edit: Why do I like includes?
Include code:
!--#include file="inc_footer.aspx"-->
the same code for a control. First you need one of these
<%# Register TagPrefix="a" TagName="HeyFooter" Src="inc_footer.ascx" %>
and then you can use it like this
<a:HeyFooter runat="server" />
this is kind of long for what I need.
Note Two security concerns with includes: 1) don't use the .inc extension, since it can be browsed. 2) do not include filenames based on user variables, as the best answer points o ut.
If you include a file via a string variable: <!--#include file=some_variable -->, then depending on how that variable is filled there are possible attacks a hacker could do to include his own files and run arbitrary code on your machine. But as long as you use a string literal, you won't run into this problem.
I would use Master Pages in ASP.NET. This is the accepted way to have common areas of a page.
You would create a Master Page similarly as you would regular pages, then modification of each of the other pages would be minimal. Add a single line to the top of each page file, then specify the sections used.
No, you most definitely do not need to use fancy .NET web form ways of doing this, if you want to keep it simple. Just put this at the points where you want it inserted:
<!--#include virtual="../repeatStuff/fun.html" -->
The html will show up there. I gave a path one up and down another directory. This is "easiest", but also has the virtue of being very straightforward. Note that this won't show up in your visual designer. (I never use it anyway.)
I still use includes every once in awhile for exactly the purpose you describe.
You don't really need to register a user control because it's just plain html anyway. And you don't want a master page because it's really just a snippet of html that needs to be on a few select pages.
So I've got includes like this from a glossary of help text files:
<!--#include file="~/Glossary/BusinessDetails.inc"-->
In my opinion there's nothing wrong with using old school include files for this purpose.

How to check whole website for certain conditions in rendered source of every page , automatically?

Like I want to check
on Every page <h3> tag must come after <h2> otherwise page should be marked.
like if any page has PDF then Some particular text <p>Download Adobe reader from here</p> should be at bottom of every page is this condition is not matched then page should be marked.
I want to make different type of conditions to check then want to check on whole site and if anything mismatch then report should be generated.
Do you necessarily have to use XHTML? I'd use Python and BeautifulSoup, myself.
(Edit: I was confused - I was thinking of XSLT, not XHTML, and I thought "why would you use XSLT for someting like this?". XHTML is fine, and my recommendation of Python and BeautifulSoup still stands.)
This ruby gem looks like it could be useful to you:
http://code.google.com/p/opticon/
I haven't personally used it, but it claims to basically do what you're asking for.
I've had, and still have, the same need on many of my projects. In my case I'm looking for anything with the class 'error'. This is supported by the TestPlan product in it's verification engine.
In my case, as a quick example, I have several "Web" states and my generic verify script is:
CheckNot //div[#class='error']
Now the way TestPlan works is that every state within "Web" will first run this generic verify script.
If you're interested I could help you come up with the exact syntax needed to do your check.

What do I need to do when I include output from an ASP.NET .aspx page in a HTML page? Something like <base>

I have a ASP.NET file at
http://localhost/nn/n.aspx
Output from processing that file is included in
http://localhost/index.asp.
Is there some general or specific things I should do so that the ASP.NET code sitting on the client knows were it is?
Setting <form action="http://localhost/nn/n.aspx
makes it post to the right location, but my update-panel is failing.
I think perhaps that's the ScriptPath of the ScriptManager, and i can probably figure that out.
But I am afraid there could be other issues, and i am hoping there is something like the HTML tag that i can use to tell my client code where it is.
Thanks
Olav
A). What exactly does "..Output from processing that file is included in.." mean?
B). <base> tags are evil html-smells which cause more problems than they solve and are the symptom of a failed architecture
C). What do you mean by HTML page? There's no way a normal HTML page can include .aspx content, HTML is not a server side language. Certainly there are ways and means, but you need to be specific.
D). you need to give us some more idea about your code and site architecture, there's not enough info here to work this out "my update-panel is failing" is very vague indeed. How is it failing?
Are you looking to create a server-side control? You don't include "output from processing" one ASPX file in another one. Instead, you create an ASCX file - a server side control and then incorporate that in the target ASPX file.
It is easy to do and may well be what you are looking for. However, your question is quite ambiguous so I cannot tell for sure!

Resources