Getting project web page names as an enumeration in asp.net - asp.net

I know that in markup view Visual Studio will provide you with an enumeration of all the page names in your project (add an element and see what you get from Intellisense when specifying the ImageUrl attribute).
My question is: how do I get to that enumeration?
If that's not possible, what would be the best way in asp.net to get the names of your pages without having to hard code strings all over the place? E.g., I'd like to be able to do something like this:
Response.Redirect(PageNames.Default);
(Where PageNames is an enum of some sort)
Is this possible? Thanks in advance!

Here is one suggestion...
Define a class that includes the pages you want, either manually or by reading a Site Navigation file:
static class PageNames
{
public static string Default = "~/Default.aspx";
public static string Contact = "~/Contact.aspx";
public static string About = "~/About.aspx";
}
You can use the class by calling the property name:
Response.Redirect(PageNames.Default);

Another option, that I have looked at but not tried out yet (might next week tho'):
http://blog.devarchive.net/2008/01/auto-generate-strong-typed-navigation.html
Looks very cool, uses T4 templating to generate a strongly typed navigation hierarchy.

Related

Declare Global/Site Wide Variable in ASP.Net VBHTML

After about a thousand years of building websites in ASP Classic, I am trying to learn ASP.Net using Razor. I am using Visual Studio 2012. Google seems to be of no help on some of these basic things (but, I probably just don't know the right question to ask). One of the absolute basic things I am struggling with right now is how to declare a variable that I can use across my website.
For example:
DIM DefaultColor AS String = "Green"
I have that in the _AppStart.vbhtml page and I try to access it using
<p>#DefaultColor</p>
in the Default.vbhtml, but I get an error saying that DefaultColor is not declared.
How and or where do I declare a variable that I can access across the website?
If the _AppStart.vbhtml page is the right page, do I need to add something to the default.vbhtml or _template.vbhtml page to load it?
First of all I am not sure if ASP.NET Core is available for VB.NET. At least I tried to create a new project for VB.NET but the ASP.NET Core is not available. I will tell you how you can do it using the C# template.
You can use the TempData object in order to store and retrieve data. You can set a value like this:
#{
TempData["Value"] = "Hello World";
}
Now you can display this value like this.
#TempData["Value"]
An other option is to create a static class which will contain the variable which will host the value.
namespace Sample
{
public class MyStaticClass
{
public static string Value = "Hello World!";
}
}
Now you can display the contents of the value like this:
#Sample.MyStaticClass.Value
You can also set a value and use it later.
Sample.MyStaticClsas.Value = "New Value";
I hope it helps.

ASP.NET 4 JSON DataContractJsonSerializer Lower Case

I have a class with several properties all defined in sentence case:
Public Class Provider
Public Property ProviderName As String
End Class
I need to be able to pass instances of this through AJAX which I will then be using in JavaScript to add to an array, process etc. Currently I am using the DataContractJsonSerializer to serialize to JSON and return through an ASHX handler. It may just be me being picky, but I don't really like having sentence case properties in JavaScript, I would prefer them all to be lower case, to produce the following:
alert(myProvider.providername);
With the default DataContractJsonSerializer I simply cannot find a way to do this! With JSON.NET it would appear that it is a simple task, but unfortunately I can't introduce another dependency into this project - as much as I would like to.
Does anybody know of any way to override the format of keys that are generated?
The project is using ASP.NET Web Forms 4.0.
Thanks,
Chris.
It's possible to customize the DataMember attribute to use a lowercase name. Here's the syntax in C#, I assume in VB should be similar:
[DataMember(Name = "title")]
public string Title { get; set; }

Managing Urls in WebForms

I have a large Webforms application. In many places throughout the application we set the navigation urls of hyperlinks in code behind. Hard coding string literals seems like a bad idea.
hlVideos.NavigateUrl = "/path/to/some/page.aspx";
This doesn't seem like a good idea either, since it could require me to have a constant string on every page that needs it:
private const string PathToSomePage = "/path/to/some/page.aspx";
hlVideos.NavigateUrl = PathToSomePage;
I've thought about a single class with a bunch of const strings in it that can be accessed. This seems like it would be an open/closed principle violation, requiring me to add another constant every time that I add a new page.
public class UrlManager
{
public const string PathToSomePage = "/path/to/some/page.aspx";
public const string PathToSomeOtherPage = "/path/to/some/other/page.aspx";
public const string PathToYetAnotherPage = "/path/to/yet/another/page.aspx";
}
How is everyone else handling this? Maybe I'm over complicating this, although I am dealing with a hundred urls or so with many pages referencing each url.
Consider using a resource file. That way you can maintain a consistent reference to pages, but it is easily maintainable in code and easily hot-fixed post-deployment if the situation requires.
You can try to write some T4 template to generate your UrlManager file class. Something like T4MVC. Look here for WebForms example T4Mvc web forms

ASP.NET: Can you use server tags to embed C# or VB.NET expressions into a Javascript function?

I have an enum called SiteTypes that contains several values that are all bound to a dropdown list. On the client side, I need to check this dropdown to see if the selected value is one of those enum values. I don't want to hardcode the value of the enum in the script in case it needs to change, so I want to use a server tag to get it directly from the enum itself. Conecptually, I would like to do this:
function SiteIdChanged() {
var x = "<%=SiteTypes.Employee %>";
}
The way I am doing it now is created a protected property in the codebehind that returns that specific enum value and am doing this:
function SiteIdChanged() {
var x = "<%=EmployeeSiteTypeValue %>";
}
I don't like that, though, because I have to create a special property on every page that I need to do such a check.
Is there a way to do what I want here?
Are you getting a "xxx is inaccessible due to its protection level" error when you compile or run the page? enums are public by default, classes are not. My guess is that you've defined your enum inside your page's class and you aren't explicitly marking it with the 'public' access modifier. Explicitly mark it as public or move it outside of the class and see what happens. If you're planning on using it on lots of pages you should stick the enum definition in in a file in the App_Code folder of your project.
If you don't like your current implementation I would consider using a PageMethod to compare the dropdown selection to the enum value. This approach will probably be cleaner, as you can do most of the logic server-side.
Here's a tutorial on PageMethods:
http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx
As long as your enum is marked public, you can just go with your first option. There's no need to put a property on every single page you want to retrieve the value from.
That approach is really the simplest solution for writing out server side values in your JavaScript.
You can use the Enum.IsDefined Method this well tell you if the selected value from the dropdown is actually part of your enum.
Enum.IsDefined(typeof(MyEnum), myValue)
http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx

Remove field in wsdl in Asp.net webservice

I'm generating dto classes with a template engine and would like to exclude some properties in an asmx webservice, what, if possible, is the best way to do this?
Ex:
[WebMethod]
public ProductPackages GetPackages()
{
ProductPackages packages = new ProductPackages();
packages.Packages.add(new PackageDTO());
return packages;
}
The PackageDTO contains some properties that's not relevant for this service.
But as the class can be regenerated any time i can't apply [XmlIgnore] to the fields.
So I'm looking for a way to apply a "exclude list" without touching the actual class.
Above is just an example, the template engine generates dto's for all tables in a given project, and I would like to be able to use them in services without needing to maintain a big bunch of nearly identical classes.
Just hit the same problem. You can exclude fields by marking them as internal.
public class Order
{
public double OrderPrice;
internal double ProfitMargin;
internal string TheTruthAboutThisCustomer;
}
If you don't want to return a field or property, then don't have it in the object you return! It's as simple as that.

Resources