Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Monday, October 8, 2012

URL Rewrite for hosted mvc.net site.

Recently I've been working on deployment of an MVC.NET based site to Azure. As a part of this I needed to re-map request url's from the Azure dns host name to my dns host. After a few fumbling starts I opted to use the IIS URL Rewriter module. The module is installed (by default) on Azure so it was good to go. I spent a good while looking into some of the other available options, in particular the SEO templates for lower-case and trailing-slash. I've had prior experience with url re-writing modules... not much of it good. I did manage to get a re-write rule set to trim and lower case my urls, but I felt it was too fragile to keep.
In the end I settled on just a host-name redirect.
<system.webServer>
<.other.>
<rewrite>
  <rules>
    <rule name="HostName" enabled="true" stopProcessing="false">
    <match url="(.*)" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="YOUR\.SERVICENAME\.cloudapp\.net" />
    </conditions>
    <action type="Redirect" url="{MapSSL:{HTTPS}}/YOURHOSTNAME/{R:1}"/>
    </rule>
  </rules>
  <rewriteMaps>
  <rewriteMap name="MapSSL" defaultValue="OFF">
    <add key="ON" value="https://" />
    <add key="OFF" value="http://" />
  </rewriteMap>
  </rewriteMaps>
  </rewrite>
</system.webServer>

Many thanks to RuslanY for his tips: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/.
This rule was inspired from the stackoverflow: http://stackoverflow.com/questions/2608994/iis7-url-rewriting-how-not-to-drop-https-protocol-from-rewritten-url

Of additional interest: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module

Thursday, November 10, 2011

Multiple submit buttons with MVC 3.

I recently needed to create a MVC (MVC3) view with multiple submit buttons.
A quick look around with Google and I found a couple of approaches, but I thought to myself, there must be a cleaner way.

Up till now I've been using the simple approach of creating a controller action where the method takes an extra parameter to describe which action to take like:

Controller:
public class MyController
{
public ViewResult Index() { return View();}

public ViewResult DoSomething(string action, string someData)
{
if(action == "Action1") {}
else if(action == "Action2") {}
else {/*unknown action.*/ }
}
}

xhtml:
<form action="My/DoSomething>
<input name="someData"/>

<input type='submit' name='action' value='Action1' />
<input type='submit' name='action' value='Action2' />
</form>

What I wanted was to be able to define my Controller to have multiple Actions, one for each submit action on a Form, like this:

public class MyController
{
public ViewResult Index() {return View();}

public ViewResult Action1(string someData) {return View();}
public ViewResult Action2(string someData) {return View();}
}

But alas, with MVC I couldn't construct a Route that would do this; MVC routing always uses the Forms action attribute to decide which Controller method to call.

In the end the answer wasn't that hard. I just created a smarter IRouteHander, and applied it to the MVC default route.
The HTML for the form looks almost the same, though I remove the Action from the Form's action attribute, since the route handler will look after that.
xhtml:
<form action="My>
<input name="someData"/>

<input type='submit' name='action' value='Action1' />
<input type='submit' name='action' value='Action2' />
</form>


Now the methods on my controller don't need 'special' arguments, and the world is a better place :D
Here is the custom RouteHandler:

A short list of C# coding issues.

Injecting services into entities It been bugging me for a while now that to use services in a entity can be a bit of pain. It usually starts...