Defining External Url for Url.Content() - asp.net-mvc-2

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.

Related

Where is the zend logic for deciding controller?

Ive inherited a project created using Zend.
I need to move the site to a new server, same platform but with a new base URL and domain.
Currently if I go to www.domain.com/index.php the first page of the site will be correctly pulled up. No other pages work.
On the old site, the url convention worked like this: www.domain.us/module/view
So for example the index page could be pulled up by going to www.domain.us/index/index
Im sure this is a simple issue of changing some path or setting (the include path, application path and application environment are dynamically generated and appear to be correct) but I have not been able to find anything.
Where does zend decide what controller to include? Is this url convention of using the subdomain to determine the controller standard and if not any hints as to where this logic might be?

Declare functions in 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).

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.

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.

Can you register a controller against multiple URLs in asp.net mvc?

I want to create an MVC application where by I can create areas of a site that use the same functionality but work under a seperate URL. e.g.
I want to use the same image gallery controller (type not instance) under two different URLs "/Event1/Gallery" and "ProductInformation/Gallery". However if I register this in the routes table and use the html helpers to create links would use the first registration found in the routes table as the link rather than being the actual URL that the controller is serving out at the time.
My questions are:
Is this the correct approach? If not what would be the better solution.
If it is the correct approach how do you stop the helpers from using the first registered controller name rather than the page it is on?
Thanks
Could you use named routes. That way you specify the route name instead of action, controller using the url helper and create the links yourself. Or you could create your own helper method to encapsulate each link.