SiteMapNode is readonly, property 'Url' cannot be modified - asp.net-mvc

I setup MVC breadcumbs for my website using MvcSiteMapProvider (Nuget package MvcSiteMapProvider.MVC5 (version 4.6.22)).
It works fine.
Then I want to update Url of Sitemap dynamically like:
SiteMaps.Current.CurrentNode.Url = Url.Action("Index");
Then I got this error:
SiteMapNode is readonly, property 'Url' cannot be modified
Note that I am still able to update Title:
SiteMaps.Current.CurrentNode.Title = "/Index";
Any idea?

The SiteMap is a statically cached object that is shared between all users. Technically, all of the properties are read-only at runtime. However, some of the properties (such as Title) are request-cached so you can safely update them at runtime without affecting other users.
The Url property is a special property that dynamically builds the URL through the MVC UrlHelper class (which is directly driven from your routes). It makes no sense to set it to Url.Action("Index") because that is effectively what it does just by itself (unless you are using a dynamic node provider or custom ISiteMapNodeProvider - those are startup extension points where you load the node configuration, so the properties are read-write).
You just need to set the correct controller and action in your node configuration (which could be XML, attribute based, or code based) and the URL will resolve on its own.
XML Example
<mvcSiteMapNode title="Projects" controller="Project" action="Index"/>
NOTE: You need to account for all route values in the request, either by adding them as another attribute myId="123" or by using preservedRouteParameters="myId" (which tells it to include the myId from the current request when building the URL). See this article for a detailed description of using these options.
NOTE: Setting a URL in the SiteMap configuration effectively overrides MVC support for that node. So, you shouldn't set a URL at all unless it is a non-MVC URL.

Related

Modify Swagger Definition Names for MVC API

I would like to modify the Swagger schema object names that are auto generated for my MVC application.
For example, I have number of entities like EmployeeModel or CompanyModel objects that are accessible via our REST endpoints
However, it's not necessary or desired that the Swagger definition contains the word "Model" after each entity name. They should be known only as Employee or Company -- see example in screen shot.
You have to use the SchemaId function available in your SwaggerConfig.cs file in the EnableSwagger method as follows:
c.SchemaId(t =>
{
c.SchemaId(t => t.FullName.EndsWith("Model") ? t.FullName.Replace("Model", String.Empty) : t.FullName);
});
This methods allows you to override Swashbuckle default behavior and set complex type names properly.
My sample addresses directly the removela of the "Model" part of your boject names, but could be using more complex setup.
What library are you using to build you swagger shema? Take a look at Swashbuckle . This allows to build swagger shema automatically for your app (by using xml-documentation) and is flexible for customization. See "Modifying Generated Schemas" in documentation.

MvcSiteMap: Dynamic node provider not reached

I created a project that the nodes are defined using attributes, and I set it in the web.config to scan for attributes, and it works fine.
I don't use an XML file at all.
Now I want to add a dynamic node provider, how do I do it?
Is there a way to do it without the XML (.sitemap) file?
I need to make sure it's under the root, which has been set in code using MvcSiteMapNodeAttribute attribute.
I've read the documentation and I can't really figure out where to place this line:
<mvcSiteMapNode
title="Details" action="Details"
dynamicNodeProvider="Project.StoreDetailsDynamicNodeProvider, Prject" />
What action is it supposed to point to? Additionally as said above, the root element is defined using attributes, so my question is if there is a way to avoid XML, or alternatively what's the efficient way to declare the XML (the less the better) to include my dynamic provider.
Update
I've tried the following and the node provider still isn't reached (From HomeController.cs).
[MvcSiteMapNode(Title = "Home", Key = HomeMenuKey,
DynamicNodeProvider = "Project.Namespace.NodeProvider, Assembly")]
public ActionResult Index()
{
return View();
}
Can you define it in the controller method attributes (and not use XML at all)?
For example:
[MvcSiteMapNode(Title="Details",
DynamicNodeProvider = "Project.StoreDetailsDynamicNodeProvider, Project")]
public ActionResult Index()
{
return View();
}
Seems that the dynamicNodeProvider attribute is ignored in the root node, also when it's defined in attributes.
So the only way to add a dynamic node provider under the root, is either by specifying it on a dummy action etc. or using XML.
An interesting note: the actual difference between defining in XML and attributes is that if it's defined in attributes, it (i.e. the gen. menu items) will be last in the menu, whereas when defined in XML it will be right after the root item (I guess that would be Home), Note that this is still controllable via the Order property in the attributes.
In my Web.Config, I left the siteMapFile empty, relying in what it said in the wiki page, that the default value is ~/Web.sitemap, in fact this is false (I've already corrected that in the updated wiki).
I don't think this behavior should be like this, I do think the MvcSiteMap engine should scan for dynamic node providers just as it scans for dynamic node attributes (here is the issue I posted on site).

how to do Localization for one of the #Model property in mvc3

I have started working on an MVC project and I came across some scenarios where I feel I am stuck. I need to convert the existing MVC3 site to work for multiple language.
I have one HeaderPage.cshtml and it has a view model bound to it by
#model IHeaderPage
And it outputs a property of this model:
<h3>#Model.HeaderName</h3>
I called this view from MainPage.cshtml
#Html.Partial("HeaderPage")
Now in the Controller's Action method I change the model's property
objHeaderPage.HeaderName="Fill your Registeration details";
And when i run the project i see the the text "Fill your Registeration details".
Now how can I change the text value, i.e. it should read from my resx file.
I have already created resx files in App_LocalResources folder.
I heard that, it can be done by Display Attribute.. but how do i do that or is there any other better way?
This should answer your question regarding the use of DisplayAttribute.
I use DisplayAttribute for every property of my ViewModel, but if you have to handle custom messages like "The record can not be saved because of an error...", or something similar, you can simply use
objHeaderPage.HeaderName = Resources.ResourceMessageName;
The framework will choose automatically the correct culture.
I prefer to put all my resources in a separate projects so I can deploy only the dll of the resources in case of need, but you can also think to deploy the resx files to edit them directly on the production machine. I guess it's up to what you prefer/need.
use System.ComponentModel.DataAnnotations Namespace in ViewModel.
[Display(Name="Fill your Registeration details")]
public string HeaderName{get;set;}
also you can use your resource file. Just review http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx

Preferred way to use config settings in asp.net mvc master page

My Problem: I have an MVC3 application where all views use a common master page. The master page has many links to other (internal) sites. I need to be able to change the domain of these links depending on the deployment environment (e.g. staging.blah.com, www.blah.com, dev.blah.com etc). This domain is stored in the web.config.
There are numerous ways of doing this, but I am looking for some sort of consensus as to the preferred method. Here are some options but I am open to any suggestions:
(1) reference appsettings from master page directly. This is the simplest and most common approach but I am not particularly keen on reading the web.config and concatenate the url throughout the master page code. In fact, I am not sure that I like the idea of the view accessing the web.config at all.
(2) stick the appsetting value in viewdata/viewbag using a custom action filter which reads the config. concatenate in the page as before.
(3) as (2), but inject appsetting value in via contructor injection rather than reading it within the filter.
(4) create a base class for all my strongly typed viewmodels and populate with the appsetting using a custom action filter.
(5) create an htmlhelper that takes in the path and internally reads the appsetting and concatenates.
(6) create a custom view base class, inject in appsetting value and make available as property or function that takes in path and concatenates.
Just to add that typically when the master page requires data, I like to use Html.Action, but this is not possible in the case of these URLs that are used throughout the master page.
Thoughts?
(5) create an htmlhelper that takes in the path and internally reads the appsetting and concatenates.
I would go with this one. Your custom HTML helper could look something like this:
<%= Html.ExternalActionLink(
"link text",
new { path = "/foo/bar.php" }
new { param1 = "value1", param2 = "value2" }
) %>
and could emit the following HTML:
link text
What I have done in the past is use viewdata/viewbag in my master page and populate its values in my base controller. The base controller in turn called another class to do the work of reading the values from the web.config.
This way the view is pretty clean (e.g. it does not contain code to read appsettings) and I don't need to create a base view model that matches all my views that use the master.
This approach has the disadvantage that uses a viewdata/viewbag but I decided that was OK in my case and extremely easy to implement.

How to get started with multi-tenant MVC application

I have searched for examples and found several but they are whole large projects. I am looking for some sample on how to get started building an MVC multi-tenant application. I think, the first part would be to decipher the url.
In ASP.Net this is how I did it. I got this from looking at DNN code. How would I do the same in MVC?
Global.asax
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string domainName = string.Empty;
// domaName now contains 'example' if application.Request was www.example.com
domainName = GetDomainName(application.Request);
// Using domain, get the info for example from the database
object myPortal = // get from database
// Save in context for use on other pages
context.Items.Add("PortalSettings", myPortal);
}
Then in my basepage I get the value from the context.
I think an even more robust means would be to define a custom route. In that custom route is where you extract the domain and put it into the route values.
You then can have the base controller (as Josh described) which defines a Domain property or the like and stores that value there for convenience (or just extracts it on demand; either way).
By pulling it into the route values up front like that, you can make use of that information anywhere in the app along the request path, not just in the controller, so you get more re-use out of it that way. You can, for example, make use of it in a custom Authorize-like filter to handle the user's rights to that domain, and so on.
Get the domain name. You are on the right track with the DNN code. Just poke around the Request static variable in the debugger; there's all kinds of cool stuff there.
You'll probably need a user store. I use a custom database, but you could use the Microsoft membership provider and profile provider. Make the domain a property of the user, or a property of an organization, and the organization a property of the user.
Store the user's domain in the cookie, encrypted. Read the cookie at the beginning of the request, and make the user has access to that org/domain.
Make a BaseController that extends Controller, then have all your controllers inherit from it. In the BaseController, override OnActionExecuting. This is a much easier place to do your initial request rigging than the Global.asax.cs's Begin_request, because you can define protected members which will be available form every controller.

Resources