How do I use a wildcard in the URL of my comparison for Request.URL.Host? I have a site that's been running for years with the test site showing as blue. The comparison used to use the entire URL.
#if (Request.Url.Host == "URLtest.site.com") //
{
bodyclass = "test";
}
We are adding a second test site and I'd like to set this one time. So I would like to use a wildcard. Something like this, but it doesn't seem to work. I'm sure I'm missing something simple.
#if (Request.Url.Host == "*test.*") //
{
bodyclass = "test";
}
You can use RegEx as suggested in other answers.
A simpler check would be to use string.Contains()
#if (Request.Url.Host.Contains("test.")) //
{
bodyclass = "test";
}
Please use Regex Match method
using System.Text.RegularExpressions;
var pattern = #"\w*test.\w*";
string input = "url2test.site.com";
var match = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
if (match .Success)
{
}
Related
Is there a reliable, programmatic way to determine that Microsoft Edge is the default browser?
I know one option would be to use the IApplicationAssociationRegistration::QueryCurrentDefault method to return the default application registered for http. It's unclear that the ProgID returned by this call is a fixed string though so it may not be the best way to verify that Edge is indeed the default browser.
Use the following code snippet. Haven't tested with Firefox or any of the other strange ones, but you'll get the following return values based on your default browser in Windows 10.
Chrome - ChromeHTML
Edge - AppXq0fevzme2pys62n3e0fbqa7peapykr8v
Internet Explorer - IE.HTTP
Code snippet below should work. Tested in a console app. If anyone wants a VB version let me know.
using Microsoft.Win32;
public static class BrowserUtils
{
static public string GetSystemDefaultBrowser()
{
string _retval = string.Empty;
const string userChoice = #"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(userChoice))
{
if (userChoiceKey == null)
{
_retval = "unknown-> userChoiceKey returned null";
}
object progIdValue = userChoiceKey.GetValue("Progid");
if (progIdValue == null)
{
_retval = "unknown->GetValue(Progid) returned null";
}
//_retval = String.Format("progId=[{0}]", progIdValue.ToString());
_retval = progIdValue.ToString();
}
return _retval;
}
}
Hope this helps. Healy in Tampa.
I have a single controller that I want to use for CRUD operations on two different entities which implement the same interface. I'd like for Ninject to give it a different repository based on a query string value in the URL (or maybe a different URL, routed to the same controller). Is this possible? How can I do it?
That's usually a design smell but you could define the binding like this:
kernel.Bind<IRepo>().ToMethod(ctx =>
{
var a = HttpContext.Current.Request["a"];
if (a == "b")
{
return new RepoA();
}
return new RepoB();
}).InRequestScope();
The following worked for me, Getting A Specific value from a route
kernel.Bind<IRepo>().ToMethod(ctx =>
{
var a = HttpContext.Current.Request.RequestContext.RouteData.Values["RouteDateValue"]
if (a != null)
{
return new RepoA(a);
}
return new RepoB();
})
CakePHP URL query parameters are not done in a standard fashion e.g. the params are /param1:value1/param2:value2 instead of ?param1=value1¶m2=value2
This means that the javascript location.search does not return a value.
There is a getQueryParams JQuery plugin that does what I want using location.search
I have had to modify this to use
var pairs = location.pathname.split('/');
instead of
var pairs = location.search.substring(1).split('&');
However this now includes everything except the host in the variable pairs. So I have to check for a ':' to see if it is a parameter.
This works - but is there a better (more Cake like) way of doing it? I don't want to improve on the JQuery plugin (e.g. Regex), I want to find a better way to integrate the plugin with CakePHP.
Upddate: I've removed the rest of the JQuery code as I'm happy with the jquery code, my issue is with fitting it more with cake
Is there some 'Cake like' way of removing the path to your app, the model and the controller from location.pathname so that you end up what you would normally get from location.search?
Since you're searching for a particular parameter, you can use a regular expression:
$.getQueryParam = function (param) {
var re = new RegExp(param+':([^\/]+)');
var matches = location.pathname.match(re);
if (matches.length) {
return matches[1];
}
return undefined;
}
So it appears there isn't a better way of doing it. Here is the javascript for reference:
// jQuery getQueryParam Plugin 1.0.1 (20100429)
// By John Terenzio | http://plugins.jquery.com/project/getqueryparam | MIT License
// Modified by ICC to work with cakephp
(function ($) {
// jQuery method, this will work like PHP's $_GET[]
$.getQueryParam = function (param) {
// get the pairs of params fist
// we can't use the javascript 'location.search' because the cakephp URL doesn't use standard URL params
// e.g. the params are /param1:value1/param2:value2 instead of ?param1=value1¶m2=value2
var pairs = location.pathname.split('/');
// now iterate each pair
for (var i = 0; i < pairs.length; i++) {
// cakephp query params all contain ':'
if (pairs[i].indexOf(':') > 0) {
var params = pairs[i].split(':');
if (params[0] == param) {
// if the param doesn't have a value, like ?photos&videos, then return an empty srting
return params[1] || '';
}
}
}
//otherwise return undefined to signify that the param does not exist
return undefined;
};
})(jQuery);
I am scratching my head as to why not this isn't working:
#if (Model.Guid != null) { Html.ActionLink("Fil", "GetFile", new { id = Model.DocumentID }); }
The conditional on its own is working as putting som random HTML in there instead of an actionlink works:
#if (Model.Guid != null){<span>Test</span>}
Likewise the actionlink on it's own renders without a problem.
Could anybody clue me in to what is going on here?
You need to put an # sign before the Html.ActionLink.
Like this:
#if (Model.Guid != null) { #Html.ActionLink("Fil", "GetFile", new { id = Model.DocumentID }); }
EDIT: Forgot to add that you don't need the semi colon, but you can leave it in if you want.
Given a url that follows the following pattern:
firstcolor={value1}/secondcolor={value2}
where value1 and value2 can vary and an action method like:
ProcessColors(string color1, string color2) in say a controller called ColorController.
I want the following route evaluation:
URL '/firstcolor=red' results in a call like ProcessColors("red", null)
URL '/secondcolor=blue'results in a call like ProcessColors(null, "blue")
URL 'firstcolor=red/secondcolor=blue' ends up in a call like ProcessColors("red", "blue")
Now from I think this can be achieved with a few routes, something like this
route.MapRoute(null,
"firstcolor={color1}/secondcolor={color2}",
new { controller=ColorController, action = ProcessColors })
route.MapRoute(null,
"firstcolor={color1}}",
new { controller=ColorController, action = ProcessColors, color2 = (string)null })
route.MapRoute(null,
"secondcolor={color2}}",
new { controller=ColorController, action = ProcessColors, color1 = (string)null })
This is sufficient for just 2 colors, but as far as I can tell we'll end up with a proliferation of routes if we wanted to have, say 4 colors and be able to have URL's like this:
'/firstcolor=blue/secondcolor=red/thirdcolor=green/fourthcolor=black'
'/firstcolor=blue/thirdcolour=red'
'/thirdcolour=red/fourthcolour=black'
and so on, i.e. we need to cater for any combination given that firstcolor will always be before 2nd, 2nd will always be before 3rd and so on.
Ignoring my ridiculous example, is there any nice way to deal with this sort of situation that doesn't involve lots of routes and action methods needing to be created?
First of all, if you are going to use that key=value format, then I suggest using QueryString instead of the URL.
But if not, you can do this :
//register this route
routes.MapRoute("color", "colors/processcolors/{*q}",
new { controller = "Color", action ="ProcessColors" });
Then in your ColorController :
public ActionResult ProcessColors(string q) {
string[] colors = GetColors(q);
return View();
}
private string[] GetColors(string q) {
if (String.IsNullOrEmpty(q)) {
return null;
}
return q.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
In this case your URLs will be like this :
site.com/colors/processcolors/red
site.com/colors/processcolors/red/green
In the case that we use the wildcard mapping I suppose we lose the ability to use Html.ActionLink to build our URL's for us?