ASP.NET MVC Deployed Application Server Address - c#

I have an application that sends an email to a user so that they may access a web form. In the email there is just a link to the start page of this form. Currently, I have the value for the form location hardcoded. Once the app is deployed I know it is in inetpub/wwwroot/appName, which results in a URL of serverip:appPort/appName.
What is the C# to get the serverip:appPort portion of the URL that I need?
I think that server.mappath() might work, but for some reason I can't get to the method even though I have the necessary references.
Note: I will be deploying this application on several different servers and really just don't want to have to hardcode the IP every time I re-deploy.

Try
HttpContext.Current.Request.ServerVariables("HTTP_HOST"), this should give you the host name.
this link will show you how to get all the keys you (may) need to get the port and application (if you don't already have them).
http://msdn.microsoft.com/en-us/library/system.web.httprequest.servervariables.aspx

Related

Can I use more than one email template files for User Registration functionality?

I have implemented a "User Registration" functionality using "CreateUserWizard" control in ASP.NET C#. When user is registered successfully, he receives an email. I used template file which reflects username and password along with redirected URL. As below:
http://myinternaldomain.xyz.com?changepassword.aspx?uname=abc&pwd=blahblah
Now, my issue is - the application should be accessible from outside domain firewall too. and external URL is something like http://myexternaldomain.xyz.com?changepassword.aspx?uname=abc&pwd=blahblah
Now, I have one template file and two URLs.
I am trying to develop a workaround to create another template file containing my external domain URL. But as of now there is no luck.
Can anybody advise is there any way to provide such template file so that both users (either internal or external) will get to see email with the respective URL?
Anyway you need one "universal" domain, because even if you can detect if current request was local or remote (outside domain firewall) and use one of two your templates later your url will be incorrect if user will change his/her location. So I would use myexternaldomain.xyz.com and just resolve it to your local webserver IP from your local network. If you have domain firewall then you probably have local DNS server. Or maybe there is way you can setup firewall rule so your traffic for myexternaldomain.xyz.com will be transferred to your local server, I'm not sure. Both tasks are administration-related though.

Run Custom C# Code In A "Web Site" From An IIS "Application"

I have a tough question here and I would like to tap the wisdom of the masses to ensure that I am approaching this issue in the most efficient way possible.
Goal: Move 78 web applications (all configured to be an IIS application under a root website) from a Windows Server 2003 box to a 2012 box with as little coding as possible. The 2012 box has a different subdomain "xxx2.blah.com" and the 03 server is mapped to a "xxx.blah.com" server. In short, the user bookmarks won't work once we migrate so we want to write a redirection utility to assist getting the users to the new xxx2.blach.com location without them noticing.
Current State:It is important to note that each application under the root website in IIS6 is configured to run under its own, and sometimes shared, app pools. Some of the applications have querystring values appended to the end of the .NET request that we want to retain because it affects the UI and other business logic already coded.
We were thinking of removing the files within each application to force IIS to return a 404. Once the 404 occurs, we were wanting to run our custom utility to lookup what the equivalent URL is. Since the 404 is an "error" by all intents and purposes, we were thinking that we could "handle" the error like this (ASP.NET 2.0 : Best Practice for writing Error Page)
Is it possible to write that code once, add the logic to the global.asax file in the root website, and then somehow instruct each web application under that root site to execute the code in the parent site? I know they each run under their own app pool and that may mean that we cannot pass execution off onto another application easily but I could be wrong. In addition, we are hoping to not have to copy/paste code 78 times. Any general "best practices" or advice would be greatly appreciated. Also, adjusting it on the network is not an option as the old xxx.blah.com is on a completely different network than the new xxx2.blah.com network.
#Carl
Thank you very much. I initially missed that those variables were available to me for this purpose! The final solution for me was to set the "Redirect To" textbox to "http://xxx2.blah.com$V?UpdateNote=true&$P". This enables the redirection to occur with both the path and querystring name/value pairs in tact and also allowed me to append my own value so that the application could detect it and display a "This page has moved" message to the user.
Thank you Carl! You da man.

Doubts about a Bug/Error sender via email

I'm developing a open source project. This project is getting bigger and may fail and crash as normal, so I tought of doing a crash sender to get the errors (if the user wants) via email.
But, since it's a open source project hosted in a easy to access site, I can't just hardcode the login and password of my software's email.
Do you guys have any better way to send crash reports (without having
a proper site, I'm using Codeplex) or a easy way to hide any password?
Instead of sending the email from the client program, create a web service (WCF or Web API) that it can call to send an email. Your web service will handle generating the email and will contain the login details for the email provider.
If you wanted to take the idea one step further, then your web service can hook into a database where you record various details about the crash (such as Windows Version #, relevant environment variables or registry settings etc). That'd give you an easy way to keep track of your crashes over time.

redirect to asp.net web form deployed on a different machine

net web application running on my home machine.
and i want to talk to a web form running on another machine . in my case i have also a vmware running windows xp which has also a vs2010 and have a web application created..
but when i do
Response.Redirect("localhost:1206/Default.aspx") from my home machine web application running on vs2010 i dont get the desired page running on the vmware it gives error msg
Note: I also tried the static ip address of my vmware instead of localhost in the URL..
Help plzzzzzzzz
You cannot use Response.Redirect() in this way, because the Response object does not span multiple machines.
You have a few options, at least:
Create a web service to expose the logic you have on your other site.
Create the fully qualified name of the other application running in the VM and use a hyperlink or anchor with href pointing at that location.
You can issue a bit of JavaScript on the postback to set
window.location = "http://vmserver/page.aspx";
If you are not sure how to send JS on the postback, take a look at ClientScriptManager.RegisterStartupScript in MSDN.
This does not allow you to share data between the sites, but you could use query string params to do this.
I would not recommend anything for a production site with sensitive data using only this technique.

Dropping a session variable (or the value of one) into a client's local application

I am trying to build a system where, when the user logs in to an online ASP.NET site, the User ID (that is placed in a session variable) is then stored on to a local application (c# application) for later use.
So far, the only way I can think of doing this is, when the user logs in, the User ID is stored in a text file on the client's machine. But, this would not be an ideal solution, as this means the local client application must then check the file to make sure the contents have not changed (almost every second as it is important that the client Application always has the correct User ID).
Any suggestions?
When you say "the User ID is stored in a text file on the client's machine" I deduce you mean cookies, because you simply can´t store files on client machines via web applications, unless there is some sort of ActiveX control involved.
You can perfectly store a cookie with the User Id on the client and access it with your console app, but this is not very reliable, as the user can have cookies disabled or he can clean the cookies folder and also because different browsers use different folders for storing cookies.
So my choice would rather be a storing the current logged users in a database and make the console app poll that info through a WCF service.
If you don´t want to use a Database, store an XML file on the server that could act as your database and use for example LINQ to XML to retrieve the data via that WCF service.
Other option we can equate instead of polling the info you could use WCF Duplex Services and make the WebService push that info to the client apps once a user logs in.

Categories