ASP.NET MVC getting calling URL - asp.net-mvc

In an ASP.NET MVC 4 application, I would like to get the URL of the site calling the application. That is, I would like to be able to change the web site slightly depending upon which URL it is called from, and as such need to know how to tell what the calling URL is? I would then call a different index.cshtml file depending on which site it is called from.
Any help would be appreciated

I'm not completely sure what you want to do, but would this do the trick for you:
Request.UrlReferrer
Or
Request.ServerVariables["HTTP_Referer"]
Although I'd prefer the first one.

Related

Virtual directory overriding MVC route

I have a web app, A, that has a virtual directory that is also an app, B. I've tried to create an MVC route in A such that the URL appears to be in the virtual directory but is still handled by A and not B. However, it seems to be being ignored. Is there a way of making the MVC route take priority over the virtual directory?
If you want App A to override App B, you're going to have to add some Url Rewriting configuration, not MVC routing logic, which, if you're using IIS7+ which, I believe, you should be set up in the web.config file for App A. IIS has to know which application to forward a request to; by the time MVC routing is invoked, the application has already been selected at that point (you cannot unselect B once the actual application is invoked * ). You also might be able to get away with setting up the url rewrite rules in App B to point to App A, but I'm not quite sure how that'd work.
Please note, what I'm saying about application invocation is very likely not strictly true. But to be honest, I don't know if I could give a completely accurate description of the lifecycle of a url rewritten request; I just hope I'm roughly approximating what happens in simplified terms. Just learn more about url rewriting in IIS and IIS7 request lifecycles visit the following links:
http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module
http://msdn.microsoft.com/en-us/library/bb470252(v=vs.100).aspx
So you got two apps A & B. You say you tried to create MVC route but in which app? If you can explain a bit more what you trying to achieve, it will be easier to answer.
ASP.net routes (also available in web forms) can take any form you want. The one thing you must keep in mind that the route maps are greedy. So if most generic route is found first it is not going to check anything below no matter what. So the order should be most specific and last one should be most general.
Check this post http://yogiorchard.azurewebsites.net/archiving-items-and-a-routing-lesson
Hope this helps

Do a full non-ajax post from a partial view in asp.net mvc

This more of an out of interest question than an urgently need an answer one, but I have been trying to find a good example of how to deal with a full postback from a partial view in asp.net MVC. The obvious example is the case where you have a small login form on every page. You can easily accomplish this through an asynchronous post back using jquery, but I am wondering if there is a way to do it without the use of javascript. I know it may be pedantic, but I don't like the idea of assuming the client has javascript enabled, particularly in this day and age where responsive design/ progressive enhacements are the big buzzwords around, so having you log in tied to javascript means that anyone on a simple mobile device won't be able to use it since their device probably won't support it.
Anyone have any ideas / examples of how to accomplish this? It's such a simple thing to implement in web forms I can't believe it's as tricky as I've heard it made out to be in MVC.
You just need a form within the view, that's all. The form will POST to its controller action method and generate a full page refresh (if that's what you mean by a full postback - I guess it is) irrespective of whether its a partial or not.
You can have multiple forms on a MVC view, and each one of them will give you a full page refresh, whereas with WebForms the pattern was one main form per page.

ASP.NET MVC generate subdomain URL in View

How do I generate a subdomain URL in the view? For ex, My application is on www.lol.com, which could change sometime in the near future. I want to link to email on google apps located on the subdomain mail.lol.com.
How do I accomplish this so I dont have to hardcode an absolute URL?
ASP.NET MVC isn't going to handle this for you.
Luckily, you're not the first one to want to do something like this. Since you have full control over routing in .NET MVC, you just have to customize something for yourself.
If you want something simple that will take out the www and replace it with mail (or append mail if there is no www), then you can check out the Request.Headers["host"] value and modify it as necessary.
If you actually want something flexible to handle the routing for you then you could check out this post on how to get started:
Hanssens.org | ASP.NET MVC Subdomain Routing
You can check Request.Headers["Host"] to find the current hostname.
You may want to strip out the www., if present.

ASP.net web form - post data to a controller in an MVC site

This may be a stupid question, but anyway... I have an MVC site and a legacy ASP.net web forms site. I have a controller action on my MVC site that I would like to (programatically) POST to from my web forms site.
I can find lots of information describing RESTful services etc., but I can't seem to find a resource that explains how to do this bit - anybody point me in the right direction?
In MVC, there is nothing special about the FORM on the page (unlike WebForms).
Just create a normal HTML FORM (without runat="server"). Set the action to point to your controller action. Set the method to POST.
That's it. In your controller action, you can access the FormCollection directly, or you can attempt to use parameter/model binding.
This post demonstrates how to post data to web server with HttpWebRequest.
You just need to compose your own data to be posted, and change the url that the data will be posted to. It's the url of your controller in your case.

ASP.net MVC HomeController URL

By default the MVC Preview 5 web project comes with a HomeController.vb class. This sets the web URL to http://www.mywebsite.com/home/ by default. If I just wanted this to be http://www.mywebsite.com/ by default, how would I accomplish that?
Answered already so I'm just going to direct you to How do I get rid of Home in ASP.Net MVC?.
Users with 10k+ rep can also refer to https://stackoverflow.com/questions/33861/aspnet-mvc-routing-basics-root-route (deleted)
I'm not sure I understand your question, if what you want is to go to http://www.mywebsite.com/ and not have it be trailed by /home, that is the behavior you will get.
Is there something else you were looking for?

Resources