Declare functions in ASP.NET WebPages - asp.net-webpages

Is it possible to declare functions (as you would do in C# not javascript) or create classes in ASP.NET Web Pages?
How about in MVC, in the View files?
If not, is there anyway to do event handling in Web Pages? (Again, NOT javascript eventhandling)

You can create your own functions within .cshtml files. This article explains the functions keyword and how to use it: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-#Helpers-and-#Functions-In-WebMatrix. If you include it in the .cshtml file that also makes use of the function, you cannot call it from other pages. If you wanted to do that, you can add a function to a .cshtml file and then put that in App_Code.
There are no events as such in ASP.NET Web Pages. You can check to see if a page has been posted back using the IsPost property.
If you want to make use of reusable utility methods in MVC, you are better off taking the more traditional approach of creating static classes and adding your methods there. App_Code is really for Web Site projects (ASP.NET Web Pages) as opposed to Web Application projects (whcih is what MVC apps are).

Related

Where to place code shared between two pages in an ASP.NET Web Forms?

I have an ASP.NET Web Forms application.
In this application I have a Login.aspx page with code behind. Now I would like to create LoginLight.aspx lightweight version of Login.aspx page.
The light version should have less HTML content and should use the key functions of Login.aspx.vb code behind. These functions have a lot of code.
Therefore, in order not to have a lot of duplicate code, I would like to move the common functions to a common file. I was thinking about placing those functions in a class library in the App_Code folder but I read on the web that is not a good solution.
I also read that to have the same code behind for two different pages because it is not a good practice. How would I tackle this issue?
Create a base page that has all the functionality that you want shared.
Login.aspx and LoginLight.aspx will both inherit that base page.

Sitefinity and custom development

We are considering purchasing Sitefinity (or DotNetNuke) on pretty short notice and there are is a question I have that I am having trouble finding a quick answer to. (I have a separate but similar post with DotNetNuke as the focus, if you can answer that better or in addition.)
We are currently not using any CMS at all and we have some custom development that will not go away just because we go with a CMS for some or most of our site.
Our custom development is c# ASPX with Site Master and nested Site Master pages. These custom apps do not own their own top level in our web site, but are part of a branch, typically one or two levels down (for example, http://www.contoso.com/branch/app/default.aspx).
How is Sitefinity typically configured in a CMS/Custom “mixed mode”? For example, is Sitefinity installed at the “top” of the web site, or “where needed” down in the web site.
How does this relate when mixing CMS and custom web applications?
Does the CMS interface allow for adding these custom apps or do you just go to the web server and add them to the structure?
It appears from reading other posts, we can create our own custom c# modules and have CMS editors “drop in” the modules on the pages. Can someone confirm that for me?
If I did not provide enough detail, please feel free to ask for more.
We have Sitefinity installed at the root with plenty of subfolders containing custom pages. We typically build a usercontrol, add it to the sitefinity ToolboxesConfig.config file, then drag/drop it into a sitefinity CMS page. I believe this is what you're talking about regarding having "CMS editors drop in modules on the pages".
We also have master/child pages that are imported as page templates into Sitefinity and they work well. You can also link from any CMS page to a regular .aspx page out in a subfolder of your own and implement code there that uses the sitefinity API, but we personally found this much more tricky and veered away from this approach whenever possible.
So in a nutshell, custom code is relatively easy to integrate into sitefinity as long as you can condense it into a user control or master page.
Disclaimer: This was all done with regular web forms development. I'm not sure how an MVC site would interact with Sitefinity (which I believe is built using an MVC architecture itself anyway).
The part of your question I'm unsure of is regarding the custom webapps in nested branches. I would recommend installing the trial version of sitefinity and trying to get a single branch integrated as a prototype. This might take a day, but it would be worth it to know if the sitefinity route is going to give you heartache.
Full disclaimer: I work on Sitefinity.
First, let me say that throughout the history of Sitefinity, extensibility and developer-friendliness have been our top priorities. We've tried to create a CMS that uses concepts familiar to ASP.NET developers and build on top of them.
Now let me go through each of your points.
You can organize Sitefinity deployment however you like. In your case with sub-apps, you can either create virtual directories in IIS and deploy separate instances of Sitefinity in each of them, or you can use the CMS to do the sub-apps. In the CMS they can be separate branches of pages, have separate templates applied and feel like separate sites. You'll still manage them through one interface. It is up to you to choose an approach.
Sitefinity is a normal ASP.NET web application with the CMS assemblies in the BIN folder. You can open the project in Visual Studio and run it like a normal ASP.NET Web app. From a developer viewpoint, there's no difference between a custom app and a Sitefinity app - they are just ASP.NET web apps.
If you follow an approach of installing Sitefinity in the root and then using the CMS to create sub-apps, you can do it through the UI. If you choose to run separate virtual directories, you have to manually upload the files. Also, Sitefinity has a built-in File explorer, which you can use to manage the files on the server.
The modules you are referring to are called widgets in Sitefinity. For a developer, they are nothing more than custom ASP.NET controls and user controls. The Sitefinity page editor UI allows you to drop widgets in placeholders on a page and configure their properties. The placeholders are defined in a masterpage (a normal ASP.NET masterpage) or through the UI.
It is very easy to create custom widgets. Here's a Quick Start: http://www.sitefinity.com/blogs/gabesumner/posts/11-08-29/ldquo_hello_world_rdquo_guide_to_custom_sitefinity_widgets_amp_controldesigners.aspx
We also have modules, which are a little more complicated concepts. They manage data, have backend and frontend UI. I encourage you to go through our blogs and documentation for further info.
We also have an SDK, containing samples with source code for extensions to the CMS.
You may want to look at Kentico CMS that supports similar scenarios - you can easily mix your own ASPX pages and CMS-controlled ASPX pages, so it's very flexible.

ASP.NET MVC - Simple Breadcrumbs (SiteMap)

I have developed a ASP.NET MVC 2 application and I want to put a simple breadcrumbs (sitemap) in each page like this:
Home > Movies > Details
It is equal the URL: http://localhost/home/movies/details
How can I achieve it? I would like to put it in my master page.
Thanks!
I would recommend using MVCSiteMapProvider. It's available as a NuGet package.
It can be used to generate breadcrumbs (which you are probably asking about) and also site maps.
MvcSiteMapProvider is, as the name
implies, an ASP.NET MVC
SiteMapProvider implementation for the
ASP.NET MVC framework. Targeted at
ASP.NET MVC 2, it provides sitemap XML
functionality and interoperability
with the classic ASP.NET sitemap
controls, like the SiteMapPath control
for rendering breadcrumbs and the Menu
control.
Based on areas, controller and action
method names rather than hardcoded URL
references, sitemap nodes are
completely dynamic based on the
routing engine used in an application.
The dynamic character of ASP.NET MVC
is followed in the MvcSiteMapProvider:
there are numerous extensibility
points that allow you to extend the
basic functionality offered.
If it is always equal to the URL, the absolute simplest thing would be to make use of that by using something like this:
var menuitems = Request.Url.AbsolutePath.Split("/".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
menuitems would now contain the menu items you need to perform a simple foreach loop and build your menu.

Defining External Url for Url.Content()

Is there any way to alter the behavior of Url.Content rendering mechanism so that my static content in the page is loaded from an external server?
To explain further, suppose you have an ASP.NET MVC 2 website, http://www.example.com and at some point, your want your static content to be loaded from static.example.com . But you have coded your application with syntax like Url.Content("~/Content/images/a.gif"). From that point, is there some configuration change within the ASP.NET MVC to render these URL's as "http://static.example.com/Content/images/a.gif"?
Thanks in advance.
Unforunately there is no such configuration setting. Your best approach would be to write your own helper extension method hanging off of UrlHelper that you control.

Useful Silverlight class library with reference to .NET class library

I'd like to develope simple set of controls to display many sort of data and be useful in normal silverlight application and can be used in ASP.NET MVC2 application. Right now i have no idea how to link those tehnologies, but this isn't my concern right now - i just heard it is possible and i'll find out how to do that...
Right now i'd like to know how can i use my Model's classes in such SIlverlight class library. I've generated POCO classes in EF4.0 for ASP.NET MVC2 application. Everything works great, but now i'd like to use some of those classes - create silverlight's controls, some ViewModel classes and fill my controls with data passed from my Model, used in ASP.MVC's app, through ViewModel.
Everything should be tested in any simple silverlight's app and at the end i'd like to be able use this class library in silverlight app and in ASP.MVC 2 website...
Silverlight it's own runtime and in general you cannot mix normal .NET assemblies and Silverlight assemblies.
However, one option is to share the source code by linking the same source file to two projects that compile to the different runtimes.
Another option is to use WCF RIA Services where Visual Studio will code generate your model classes in the Silverlight project and there are also options for sharing code between tiers. You will also get a service to operate on the objects from Silverlight. The service can be built on top of EF4.0 with very little code.
Finally, Silverlight 4 and .NET 4 are in some cases able to share assemblies if certain criteria are met. See Stack Overflow question named Silverlight 4 Assembly Sharing Problem.
What you need to do is use shared code.
you do this by adding an existing file to a project. In the add file dialog there is a arrow on the button that exposes the add as link option.
the point of this is that you cna have 2 projects which target the different CLR's but contain the same code and namespace references ... you can leverage these to have code that works on both versions of the CLR very simply.
Its really quite powerful. I have one set of tests that can target both versions.
Of course you are limited to the set of common functionality but that is implicitly part of your requirements anyway.
You cna even pass the objects over a service and have them deserialise nicley into the other CLR. Kinda suprising how well you can use the symetry
I am currently using this for aproject and am achieving what you want and after a few hours of playing with really suprisingly painless.
regards