Why my redirect is not working...? - asp.net-mvc

I am trying to make a redirect from my not www domain to my www domain like this:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().Contains("http://mydomain.com"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", "http://www.mydomain.com");
}
}
For some reason the redirect is not done...(ok, I know this can be done by defining a CNAME also but I am simply wondering why this is not working...
I think it's something small buggy here...but I am not able to see it.

Use this tool to verify. May be you have to change code little as below,
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.ToString().Contains("http://mydomain.com"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://mydomain.com",
"http://www.mydomain.com"));
}
}

Related

Xamarin.Android , OnBackPressed() method handling

I have main_activity and dashboard_activity, on dashboard_activity I added fragment using code in OnBackPressed() method
public override void OnBackPressed()
{
//base.OnBackPressed();
FragmentTransaction transaction =FragmentManager.BeginTransaction();
Dialog dialog = new Dialog();
dialog.Show(transaction,"dialog_fragment");
}
My fragment code is here
class Dialog:DialogFragment
{
private Button btnExitapp;
private Button btnLogOut;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.dialog, container,false);
btnExitapp=view.FindViewById<Button>(Resource.Id.btnExitapp);
btnExitapp.Click += BtnExitapp_Click;
btnLogOut = view.FindViewById<Button>(Resource.Id.btnLogOut);
btnLogOut.Click += BtnLogOut_Click;
return view;
}
private void BtnLogOut1_Click(object sender, EventArgs e)
{
}
private void BtnExitapp1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
Note:My code for btnExitapp work fine only once after deployment and if I reopen the app in emulator and press btnExit it close app and reopen with Dashboard_Activity instead of closing app.
Please guide me to achieve
that when BtnLogout pressed, should go back to Main_Activity as it work on backpress button and when BtnExitapp pressed should close the app
At first, if you want to go back to the previous activity in your dialogframent, you can try to call the base.OnBackPressed(); in the private void BtnLogOut1_Click(object sender, EventArgs e). In addition, maybe you need to call it twice, from dialog to dashborad activity and then the main activity.
Then if you want to exit the app, you can use the following code:
private void BtnExitapp1_Click(object sender, EventArgs e)
{
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
}

Can't remove http header "X-AspNetMvc-Version"

I try to remove the Http header X-AspNetMvc-Version. I added this in Global.asax:
void Application_Start(object sender, EventArgs e)
{
MvcHandler.DisableMvcResponseHeader = true;
}
but it doesn't work. In Firefox I can see X-AspNetMvc-Version: "5.1".
What am I doing wrong?
You might need to just stick this into your Global.asax.cs:
MvcHandler.DisableMvcResponseHeader = true;
Check to see if you have anything configured in the HTTP response headers in IIS.

Click handler not working, possible unintended reference comparison

this doesnt work ,i have an item "Area" in my listBox1,but code is not producing MessageBox in wp7 runtime,
private void button1_Click(object sender, RoutedEventArgs e)
{
if( listBox1.SelectedItem=="Area")
{
MessageBox.Show("hi");
}
}
try:
listBox1.SelectedItem as string == "Area"
You are missing the cast.

Login to another website and parse data

There is php page, let's call it http://www.aaaa.org/login.php which has two textboxes (name="username" and name="password") and a button.
And there is my .aspx page from which I want to login to that .php page. Once logged in I want to retrieve and parse some content of a subpage http://www.aaaa.org/details.php?id=1234.
I tried some code but it always retrieves the content of the login.php page so I assume I can't login at all.
Can you show me the exact code how to do this?
The language is C#.
To do this, you can use an HTTPClient library. Something like this:
http://www.codescales.com/
This will enable you to login and access the client application. Note that your application will be fragile and subjected to the web app and it's changes. So if they change the field names, your app will have to change as well. But it will work for what you are trying to achieve.
I would also suggest writing this as a separate library and not directly in your ASPX code.
protected void Page_Load(object sender, EventArgs e)
{
WebClient req = new WebClient();
CredentialCache myCache = new CredentialCache();
//myCache.Add(new Uri("http://www.aaaa.org/login.php"), "Basic", new NetworkCredential("login", "passw"));
myCache.Add(new Uri("http://www.aaaa.org/login.php"), "Digest", new NetworkCredential("login", "passw"));
req.Credentials = myCache;
string results;
results = System.Text.Encoding.UTF8.GetString(req.DownloadData("http://www.aaaa.org/login.php"));
}
protected void Button1_Click(object sender, EventArgs e)
{
string TheUrl = "http://www.aaaa.org/details.php?id=2923";
string response = GetHtmlPage(TheUrl);
TextBox2.Text = response;
}
---------------- updated: -----------------
protected void Page_Load(object sender, EventArgs e)
{
WebClient req = new WebClient();
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("http://www.aaaa.org/login.php"), "Basic", new NetworkCredential("login", "passw"));
NameValueCollection postData = new NameValueCollection();
postData.Add("username", "login");
postData.Add("password", "passw");
req.UploadValues("http://www.aaaa.org/login.php", postData);
req.Credentials = myCache;
string results;
results = System.Text.Encoding.UTF8.GetString(req.DownloadData("http://www.aaaa.org/login.php"));

Problem with SaveFileDialog in Silverlight 3

i have weird exception by SaveFileDialog in Silverlight 3. I don't really have a idea where the problem is.
I create instance of SaveFileDialog in Loaded event of user control. After Download button is clicked and dialogResult is true asynchronous file download is started. After file download is completed, method OpenFile() is called. This works fine once, but second time I get exception:
Exception message:
"No file was selected"
Details:
{System.InvalidOperationException: No file was selected.
at System.Windows.Controls.SaveFileDialog.OpenFile()
at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)}
Stack:
at System.Windows.Controls.SaveFileDialog.OpenFile()
at Spaces.Client.Views.Dialogs.FileDialog.BL_DownloadFileCompleted(Object sender, EventArguments`1 e)
at Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler.Invoke(Object sender, EventArguments`1 e)
at Spaces.Client.BL.WebService.SpacesService._spacesService_DownloadFileCompleted(Object sender, DownloadFileCompletedEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Spaces.Client.BL.SpacesServiceReference.ServiceClient.OnDownloadFileCompleted(Object state)
Here code snippet:
private void _userControlFileDialog_Loaded(object sender, RoutedEventArgs e)
{
_comboBoxVersions.ItemsSource = _file.Versions;
if (_comboBoxVersions.Items.Count > 0)
_comboBoxVersions.SelectedIndex = 0;
String extension = "*." + _file.Extension;
_sfd = new SaveFileDialog();
_sfd.DefaultExt = _file.Extension;
_sfd.Filter = extension + "|" + extension;
}
private void _hyperlinkButtonDownload_Click(object sender, RoutedEventArgs e)
{
string path = ((FileVersion)_comboBoxVersions.SelectedItem).Url;
bool? dialogResult = _sfd.ShowDialog();
if (dialogResult == true)
{
AppContext.BL.DownloadFileCompleted += new Spaces.Client.BL.Interface.DownloadFileCompletedEventHandler(BL_DownloadFileCompleted);
AppContext.BL.DownloadFileAsync(AppContext.AuthenticatedUser, path);
}
}
void BL_DownloadFileCompleted(object sender, Spaces.Client.BL.Interface.EventArguments<byte[]> e)
{
byte [] data = e._result;
using (Stream fileStream = (Stream)_sfd.OpenFile())
{
fileStream.Write(data, 0, data.Length);
fileStream.Flush();
fileStream.Close();
}
}
Have anybody idea what is wrong?
Regards
Anton Kalcik
There was problem with multiple event handlers. On each click is event handler attached and never detached. Event handler stays attached also after UserControl is closed. So it is on developer to detach event handler on properly way.
Regards
AKa

Resources