If Referer = "/Search", Do X, Else Do Y: "Bad Argument (Expected Uri Object or Uri String)"

If referer = /search, do X, else do Y: bad argument (expected URI object or URI string)

@referer = URI(request.referer).path

The URI method expects a "URI object or URI string." But if there's no referer, then you're not giving it either of those. This is equivalent to calling URI(nil).path. A quick fix would be:

@referer = request.referer && URI(request.referer).path

This way, if there's no referer, @referer will be set to nil, and your if statement will work as expected.

I'm learning Rails with M. Hartl's Tutorial. Ch. 11.2.5 and got this error in tests: ArgumentError: bad argument (expected URI object or URI string)

See if this helps:
RSpec test destroy method (Rails Tutorial 3.2 Ch. 9, Ex. 10)

I'd also read further down in the link above; you should make maximum use of Capybara instead of mixing rspec with methods provided by Rails for Action*::TestCase. Do this instead

you can use capybara's page.execute_script (but you have to enable
javascript for this example :js => true)

How to remove illegal characters from path and filenames?

Try something like this instead;

string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
illegal = illegal.Replace(c.ToString(), "");
}

But I have to agree with the comments, I'd probably try to deal with the source of the illegal paths, rather than try to mangle an illegal path into a legitimate but probably unintended one.

Edit: Or a potentially 'better' solution, using Regex's.

string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
illegal = r.Replace(illegal, "");

Still, the question begs to be asked, why you're doing this in the first place.

OData Error: The query specified in the URI is not valid. The property cannot be used in the query option

From the docs 13.1 Model Bound Attributes:

Now the default setting for WebAPI OData is : client can’t apply
$count, $orderby, $select, $top, $expand, $filter in the query, query like localhost\odata\Customers?$orderby=Name will failed as
BadRequest, because all properties are not sort-able by default, this
is a breaking change in 6.0.0

So, we now need to enable OData Model Bound Attributes which you can do globally with the middle line in the following block (the other two are your code):

ODataModelBuilder builder = new ODataConventionModelBuilder();
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); //new line
builder.EntitySet<DB.Project>("Projects");

But that is a catch-all and kind of works around the better security/performance this change brings.

So, you can, and maybe should, enable OData Model Bound Attributes using fluent API calls per entity like this:

builder.EntitySet<DB.Project>("Projects"); //your line of code
builder.EntityType<DB.Project>().Filter("ProjectID");

This answer should solve the problem you posted about but, I expect, you will need to take a look at those docs to enable you to work up a comprehensive solution for the rest of your project (unless, of course, you just deploy the one-line catch all!).


As the name "Model Bound Attribute" suggests, you can also achieve what you need via attributes on your models, which is covered in (in fact, is the main focus of) the docs too.


Edit February 2017:

There appears to be a bug in the per-entity fluent API. Calls to $expand entity-sets intermittently return a 400 Bad Request with the error in the original question despite the entity sets being set up with fluent API. I don't know whether this bug only exists on $expand or with other query params. I also don't know whether it is my code that is causing the problem or an MS bug and therefore something others are encountering. I will investigate this further soon and update this answer. For now I am using the one-line catch all; that works just fine.

Further edit:

I have just reread some of the docs (to try and get this update as understandable as possible) and they seem to imply that the way I now have things set up (with the Global Config one-line-catch-all plus fluent API), the per-entity fluent API will still be respected because:

"Query settings can be placed in many places, with the following
precedence from lowest to highest: System Default(not query-able by
default), Global Configuration, Model Bound Attribute, Fluent API."

Therefore, maybe this is what you have to do: add the one-line-catch-all and then fine-tune with model-bound-attributes, fluent API or both. I need to test this and will report back soon...



Related Topics



Leave a reply



Submit