Skip to content
Artem Smirnov edited this page Sep 5, 2013 · 8 revisions

A couple of Q&A fetched from Google Groups and SA

Q

I just get an error in chrome like the following: This XML file does not appear to have any style information associated with it. The document tree is shown below.

A

what your are seeing is the XML representation of your model because a view couldn't be located. That's part of the built-in content negotiation. If the model had any properties on it, you would see that data serialized as XML.

Source

Q

What's the preferred way to order/reorder behaviors?

A

The "preferred" way is to use the Policies.Reorder method. Here's an example:

registry.Policies.Reorder(x =>
	    {
		    x.WhatMustBeBefore = node => node is MyNode;
		    x.WhatMustBeAfter = node => node is OutputNode;
	    });

WhatMustBeBefore and WhatMustBeAfter are just Func<BehaviorNode, bool> for a simple match. You can do whatever you want there.

Source

Q

I made a typo in my Model declaration in a Spark view, and after correcting it, the application is still accessing the previously compiled version in my Temp ASP.NET Files directory. I've cleaned the directory out, recreated the file from scratch, but it's still trying to use the old model declaration.

A

Did you cycle IIS/IIS Express (or at least "touch" the web.config)? I believe that the model declaration is used during initial bootstrapping in wiring up which views go to which routes

Source

Q

I'm trying to implement themes support, but as far as I see I won't be able too. The problem: the ViewAttacher simply ignores the cases when more than one view for one action is found.

A

Runtime attaching, no, but you can attach multiple views to the same endpoint with a condition that is evaluated at runtime to select the view (or really just a media writer). (a code sample would be cool)

Q

Is there a way to invoke a partial without a model? Or can a partial take a dynamic or an IEnumerable<> as a model? It seems awkward to define an empty class just to have a typed partial view.

A

As for dynamic, that's just really not idiomatic Fubu. To get any real advantage out of fubu on the view side you'd wanna be heavily utilizing html conventions and that means strong typed views. Couldn't you just use Razor's own partial (forgive me, but I use Spark and don't know if Razor supports that) support to render a view w/o invoking a fubu partial at all?

Source

Q

when a user registers/creates a new account, I want to log them in automatically.

A

In your registration endpoint, you could take a dependency on IAuthenticationSession and use the MarkAuthenticated method. That will propagate throughout the authentication infrastructure (potentially sending down the appropriate cookies as well).

Source

Q

If a client sends me badly-formed json, the formatter throws an exception which is unhandled, so the service returns an HTTP 500 (Internal Server Error) error. I would feel much better about returning an HTTP 400 error, since the problem was with the request and not with my handling of their request.

A

Your behavior that does the error catching/400 needs to be before the InputNode in the chain.

Source

Q

Does FubuMVC work on .NET 3.5?

A

Sorry no, it works on 4.0 and 4.5 only.

Q

if a user hits a url and he hasn't been authenticated yet, he's being redirected to the login screen. But after he successfully authenticated, I want to redirect him to the page he intended to visit originally.

A

We have on our login model a property of type string named ReturnUrl. Fubu will wire this up when the login is loaded as long as there is a ReturnUrl in the querystring. From there, on successful login we check if ReturnUrl is empty, if it is we redirect them to the home page, otherwise we redirect them to the ReturnUrl.

public FubuContinuation Execute(SignInModel model)
    {
        var loggedin = _authenticationService.SignIn(model.UserName, model.Password, true);

        if (loggedin)
        {
            return FubuContinuation.RedirectTo(model.ReturnUrl.IsEmpty() ? _urlRegistry.UrlFor<SomeHomeRequest>() : model.ReturnUrl);
        }

        return FubuContinuation.TransferTo(new SignInRequest { ReturnUrl = model.ReturnUrl, LoginFailed = true });
    }

Source

Q

How do I add a new method a la DisplayFor?

A

namespace MyNamespace
{
    public static class FubuPageExtensions
    {
        public static HtmlTag LinkTo(this IFubuPage page, string input)
        {
            return new LinkTag(input, "domain.tld?var={0}".ToFormat(input));
        }
    }
}

...and in my spark view

${this.LinkTo(Model.Something)}

Source

Q

I am trying to use FubuMVC.Authentication nuget and am confused on how I would use a custom razor view for the login page instead of the default login.

A

Create a login view that uses FubuMVC.Authentication.LoginRequest as your model. Authentication will detect you have a route and use yours. To handle the authentication portion, do the following:

Create a class that inherits ILoginSuccessHandler. Handle your login in the LoggedIn method. You can take a dependency on IOutputWriter and just do a RedirectToUrl if all is good to your home page, or you can take a dependency on LoginSuccessHandler class itself and just call _loginSuccessHandler.LoggedIn(request) when successful. Here is a gist of mine https://gist.github.com/4670722

Make sure you register your ILoginSuccessHandler within your FubuRegistry like so:

Services(x => { x.ReplaceService<ILoginSuccessHandler, YourLoginSuccessHandler>(); });

To be clear here, the only thing you have to do is create a view that uses FubuMVC.Authentication.LoginRequest as the model. The LoginSuccessHandler is just a way of customizing the authentication mechanism further.

Source

Q

I'm having issues with differentiating between things that need to be authenticated, and others that simply need to be aware whether the current user is actually authenticated. The simplest example is that my top navigation is generated in my master spark view, which neither knows or cares about the current route: if the user is signed in, it shows links specific to that user (e.g. "my ...") and a logout link, whereas if they're not signed in I have signin/create account links.

A

Personally, I would probably break out the login status to a partial with this.Partial<LoginStatusInputModel>(). I might also leverage model binding / property binder, and pull things from the ISecurityContext. This way username, and maybe IsAuthenticated are just simple values on your input model and makes testing cleaner IMO.

I'd also push you to using the built in integration of authorization rules into the Link and FubuMVC.Navigation models.

Q

I can't seem to get HTTP verb restriction working. I have a really simple action, something like:

public OutputModel PaymentInformation(InputModel data)
{
   return new OutputModel();
}

I want to restrict this endpoint to HTTP post, but when I change the method signature to OutputModel Post_PaymentInformation(InputModel data), I get a WebException: System.Web.HttpException: The HTTP verb POST used to access path '/v2/Archives/PaymentInfo' is not allowed.

I want to ignore some of the namespace text, since it's just a general company namespace, but there's an element of the namespace that I want to preserve as part of the route -- this works perfectly until I add Post_

A

That's a built in url convention. If a method starts with "get_" or "post_" or "put_" or "delete_" (I think), FubuMVC will only use the method name to determine the Url with "_" considered to be the "/" character. So "Post_PaymentInformation" would give you Url: /paymentinformation with an Http constraint of POST.

Source

Q

I have some comma separated user input in which the customer wanted to support embedded commas, and I want to parse this CSV data.

A

   var tokenizer = new CsvTokenizer();
   var values = tokenizer.Read(csvUserInput);

You can use the CsvReader (ICsvReader) to go through the entire model binding subsystem and get some SERIOUS power out of that reader: http://lostechies.com/josharnold/2012/10/19/model-binding-isnt-just-for-the-web/

Source

Q

How do I set the view profile for html conventions

A

Option 1 - Use alter settings

  AlterSettings<ViewEngines>(views =>
  {
    views.IfTheViewMatches(token => token.Name() == "HerpDerp")
      .SetTagProfileTo("HerpDerp");

    //...
  }

Option 2 - Use a policy

    [Policy]
    public class ActionlessViewConvention : IConfigurationAction
    {
        public void Configure(BehaviorGraph graph)
        {
            var attachedViews = graph.Behaviors
                .Where(x => x.HasOutput())
                .SelectMany(x => x.Output.Writers.OfType<ViewNode>())
                .Select(x => x.View).ToList();

            var unattached = graph.Settings.Get<ViewEngines>().Views.Views.Where(x => x.ViewModel != null && !attachedViews.Contains(x));

            unattached.Each(v =>
            {
                var chain = BehaviorChain.ForWriter(new ViewNode(v));
                chain.IsPartialOnly = true;

                graph.AddChain(chain);
            });
        }
    }

Q

How to add custom view attachment

A


  AlterSettings<ViewAttachmentPolicy>(views =>
  {
      //Add default policies
      views.AddFilter(new ActionWithSameNameAndFolderAsViewReturnsViewModelType());
      views.AddFilter(new ActionInSameFolderAsViewReturnsViewModelType());
      views.AddFilter(new ActionReturnsViewModelType());

      //Add custom policy
      views.AddFilter(new CrudViewConvention());
  });

Q

How can I query a request for validation rules in order to set HTML conventions?

A

https://groups.google.com/forum/?hl=en&fromgroups#!topic/fubumvc-devel/9WHYYstKKFc

Q

I'm attempting to prefix all routes within a namespace via a marker class. Is there a way to preserve the default policies (Endpoint suffix with parsing method and HTTP constraints) and just prefix the route name with something?

A

[ConfigurationType(ConfigurationType.ModifyRoutes)] // THIS IS IMPORTANT!
public class JoeyPrefixRoutes : IConfigurationAction
{
	public void Configure(BehaviorGraph graph)
        {
		// just reach into the BehaviorChain.Route property and change the pattern.
       }
}

And register in your FubuRegistry as Policies.Add<JoeyPrefixRoutes>();

Source