asp.net mvc how to create subdomain url - asp.net-mvc

I am working on site which is scattered in multiple subdomains. From the main site I have to create url which points to sudomains. How can I do this.
for example, my main site is www.mysite.com
I need to create a url pointing to blog.mysite.com/userId/Name which will point to HomeController and Index method witch userId and name.
Also, how should I handle the urls on local machine as well on the production machine.
Help will be appreciated
updating my question here.
What does
#Html.ActionLink("MyLink","Index","Profile","","www.test.com",null,null,null)
do.
It creates a link, but with port number in it. If there is anyway I can avoid it.
Regards
Parminder

As your sub domains will host their own applications with controller and actions I'd recommend to use the good old HTML hyperlink to create that link:
Your Blog
Why: Because you do not want to couple your disjoint applications and ASP.NET MVC does not have a clue (and should not have a clue) about the routes used in some other application. For constructing the link you can add an extension method to HtmlHelper to avoid typos and URL path encoding quirks.

Regarding port number-- you really need to set up a virtual directory in IIS to avoid it. And if you're goal is to have separate sites-- you're going to have to set up virtual directories for all the subsites as well. I am wondering if you really need to set up completely separate sites? How extensive will the subsites be? Will there be any connection between the "root" site and the "sub sites".
Out of curiosity--- how many subsites? If you can do it without revealing too much-- what is the end goal here?

If you are looking for string typed items you might want to use the razor #helper syntax.
You could create #helper.blogLink("Title", "Action", "Controller") that creates a link to the subdomain.
http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

Related

How to add second application to MVC website

Say I've got an MVC website, www.codesmurf.com for example, set up with default ASP.NET MVC routing, nothing fancy. So my BlogController can be found at www.codesmurf.com/blog, and my FAQ similarly at www.codesmurf.com/faq.
Now if I also have a small survey project, currently in a different solution, how would I achieve to access this survey at www.codesmurf.com/survey?
What changes would I have to make to my routing and/or project structure to achieve this? So the entire survey project would be at the controller level of the main website, judging by the uri.
Do I have to achieve this using Areas? Do I need to create a SurveyController on the main site to redirect internally? Do I need to host this website separately and redirect externally? Any IIS configurations? How do I make sure my old routing isn't messed up?
This seemed like an easy task in my head at first, but I really have no clue what the best way to achieve this would be, and questions keep popping up in my head the more I think about it.
Note that the survey site is also an MVC project with its own controllers etc..
I haven't had much experience with changing the MVC routes, but would like to understand what I'm doing as well, so context/explanation would be greatly appreciated.
I personally would add the survey functionality as a service reference to your current MVC project.
Add Areas/Survey to your current MVC project and then start using the code from the service reference as you build up the Survey area.
When adding areas, all you will need to do is add some more rules to your map routes.
routes.MapRoute("areaRoute", "{area:exists}/{controller=Home}/{action=Index}/{id?}");

How can I generate links to other MVC applications?

I've created three related ASP.NET MVC web-applications that sit in IIS like so:
root
|-Emailing
|-InternalManagement
Where the root site is customer facing.
The three sites have different security requirements and I wanted to be able to modify one application with less worry about breaking the other two.
However both the root and the internal management site need to have links to the emailing site.
I'm using T4MVC.
Now I've separated the T4MVC helpers for each project by modifying the HelpersPrefix, and root and InternalManagement reference emailing so for example I can do something like:
Url.Action(MVCEmailing.CustomerDocuments.Index())
Which almost works - except the actual URL produced will be:
For the root application:
http://mydomain.com/CustomerDocuments/Index
for the internal management application:
http://mydomain.com/internalManagement/CustomerDocuments/Index
What I need in both cases is for the URL produced to look like so:
http://mydomain.com/emailing/CustomerDocuments/Index
What's the best way to go about doing this?
Copying from the T4MVC doc:
One key concept to understand about T4MVC is that it is just a thin
layer over MVC. e.g. while it provides strong typing, in the end it's
just putting values in a dictionary, and using standard MVC to
generate the routes.
One thing I often suggest when people have questions is to first
figure out how they would do things without T4MVC. If you have no idea
how it would work, or think it may not be possible at all, then you
are probably expecting too much of T4MVC! Generally, it will not do
anything that MVC does not support.
In your case here, I'm not convinced that you could do this with plain MVC, because each of the application does not have knowledge of the other applications' routes. And generally, I don't think that MVC routing can ever general links that go outside of the current app.

Multiple URLs and single codebase with ASP.NET MVC

I'm pretty new to ASP.NET MVC and I just want ask of this scenario is possible and, if so, could anybody provide any resource links on how to implement it.
Say I have a site that can be accessed from www.mysite.com, can I also have the same site load up through www.mysite2com, www.mysite3.com and so on? effectively providing the ability to run multiple sites from a single code base?
The idea is to have the site content and style sheet change depending on site visited but keep the structure the same.
thank you very much for any help you can provide :)
Kris
Yes, this is possible
http://web.archive.org/web/20100119084358/http://just3ws.wordpress.com/2010/01/03/skinning-your-asp-net-mvc-application-based-on-your-sub-domain
This example uses subdomains of the same domain but nothing stops you from using the same logic and have different images/CSS/paths etc generated based on full HOST/domain name

Azure virtual paths for localization

I would like to provide localization for my website in Azure.
I went with a classic aspx website since localization is supported by classic asp out-of-the-box and I have only a few pages. I want to keep it simple so MVC might be overkill.
I plan to register only 1 Azure web role for the site (foo.com) but would like virtual paths for localization, eg. foo.com/de-de, etc.
Azure does not allow virtual folders like IIS, so I think I can use Request.Path and do some jugglery to detect the virtual (localized) path.
Can you think of any other clean method? Maybe some web.config tags?
Thanks
Maybe you can do something with ASP.NET routing?
yes, sorry I did not see your response earlier and that's exactly what I used.
I was not aware that we can use routing in web forms:
http://msdn.microsoft.com/en-us/library/cc668177.aspx
I defined my localization pattern in the RegisterRoutes, like:
routes.MapPageRoute("loc",
"{language}-{country}/{action}",
"~/default.aspx", true)
Rather than page load, I use the InitializeCulture() event and set the thread culture based on the values as derived from the route spec., e.g. string lang = Page.RouteData.Values["lang"].ToString();
Remember to check for nulls, etc.
Think of putting this in a base Page class of yours.

Nested Applications with ASP.NET MVC

I'm currently working on an asp.net-mvc content management system. It would be incredibly useful to be able to deploy nested applications e.g. /shop to have a separate app inside. Or even another instance of the cms.
I've found some information around talking about limiting the inheritance of the web.config but I've no idea how this maps to an MVC application. I'm hoping as its essentially the same stack it will just run. Nothing is ever that easy though.
If anyone has any experience doing this I would be really grateful. The idea of one big application to rule them all is not at all nice.
Si.
To be honest you biggest hurdle is going to be creating the routes and making sure they don't interfere with routes already in the system. After you get that working the rest is easy as pie.
The first thing you will need is an HttpModule that will be inserted in to the web.config under the . This module will be used to register and custom ViewEngines or Routes that you want to register. You do this in the same way that you do in the Global.asax but instead of putting them in the Application_Start you put them in the static constructor of the HttpModule. This is so they are only loaded once like Application_Start.
By do the above you will have created a module that is easily transportable and doesn't require the implimentor to modify their Global.asax code to get your stuff to work.
The second thing you probably want to do is create a custom configuration in the web.config to set stuff like the root path of your application. This will be prepended on to the route when you are setting it up in the HttpModule. Also you can use this to store customization information that is not appropriate for the database.
Last but not necessary is that you may want to create a custom ViewEngine that knowns and understands your folder structure. This is only necessary if you want to store the views in a different path than the default views, in order to minimize conflicts.
Check out the Grouping Controllers with ASP.NET MVC from Phil Haack.
Hope it helps,
Bruno Figueiredo
I've gone down this road before (with /blog), but found it to be doable but complicated and difficult to maintain. Instead I ended up using subdomains:
www.example.com
shop.example.com
blog.example.com
These are much easier to maintain because you can just have them work as separate websites in IIS. And, of course, you can always redirect www.example.com/shop to shop.example.com.

Resources