Access MVC application settings from layout page(s) - asp.net-mvc

I am trying to further help my organization's users know that the environment they are on is our test environment. I have putting a setting in the application settings section of both the web.config and then transform(replace) that setting in the web.release.config. All I want to do is switch our logo from it's normal color to a red variant. Below is what I've tried and it always goes to the else statements option.
#if (System.Configuration.ConfigurationManager.AppSettings["Environment"] == "PROD")
{
<img src="~/Content/Img/default/sru_logo.png" class="sruLogo" alt="" />
}
else if (System.Configuration.ConfigurationManager.AppSettings["Environment"] == "PPRD")
{
<img src="~/Content/Img/default/sru_logo_test.png" class="sruLogo" alt="" />
}
else
{
<p>Not Working</p>
}
I've tried using Context.Application["Environment"], System.Web.Configuration.WebConfigurationManager.AppSettings["Environment"]
None of the above has worked and as I stated all I am trying to do is use a different image on my _Layout.cshtml file based on an application setting.

try using round brackets instead of suqare
System.Configuration.ConfigurationManager.AppSettings("Environment");

Related

how doI display image from images file within my wwwroot on my cshtml

The src route is correct. Instead of the image, I get the default 'green hill blue sky' file image on my cshtml
<img src="../../wwwroot/images/images.jpg" width="30" height="30" alt="">
What I tried:
changing it from .jpg to .png
got the same results
Controller that loads the page:
basically, all it does is checks if a user is logged on or not by checking the session. Unsure if I should be doing anything else to also display the images. fairly new to this framework.
[HttpGet]
[Route("dashboard")]
public IActionResult Dashboard()
{
if(HttpContext.Session.GetInt32("UserId") == null)
{
return RedirectToAction("Index","Home");
}
ViewBag.loggedinUser = HttpContext.Session.GetInt32("UserId");
HttpContext.Session.SetInt32("DashboardTab", 0);
return View("Dashboard");
}
The issue is the link to your image.
Try ~ instead of "wwwroot"
Firstly,check if you have app.UseStaticFiles(); in your Startup.cs.
Then try to use
<img src="~/images/images.jpg" width="30" height="30" alt="">
the tilde character ~/ points to the wwwroot.Refer to the official doc.

Image doesn't show even when I can see it doing inspect in the browser

I have tried for more than two hours and I can not find an answer to this problem. I have this part of the code in a view strongly typed in MVC:
<td>
#foreach (var TitleBook in Directory.GetFiles(Server.MapPath("~/App_Data/Images"), "*.jpg"))
{
var fileName = Path.GetFileName(TitleBook);
if (Convert.ToInt32(fileName.Substring(0,3)) == item.IdBook)
{
<img src="#TitleBook" alt="Alternate Text" height="100" width="100">
}
}
</td>
The name of the image file is created so the three first characters in the filename are numbers.
When I run the code, I get the alternate message for the image in the hmtl view. However, the path to this image is well read and I know it because I can obtain it by doing inspect in the browser and calling only the src part of in another window which shows the image as expected (sorry for my English). The css is handled by the version of boostrap installed in VS2017.
Could someone point to my error here?
Your titleBook is a full physical path. But <img> wants an URL.
Untested:
<img src="~/App_Data/Images/#fileName" alt="Alternate Text" height="100" width="100">
BTW: with Directory.GetFiles(..., $"{item.IdBook:D3}*.jpg")) you wouldn't need the foreach loop.

UMBRACO: How to add image on template

So, I'm currently new to Umbraco and I'm following some tutorial videos on umbraco.tv. I have created my template and is currently using it to setup the Home Page for my website. I have all the front end working and its displaying properly.
My problem is on the images. I'm using a lot of images for my website and I'm trying to link it to content so the images can be easily changed(not sure if I'm making sense).
Navigation bar image
If you click the link above, that's how my current navigation bar look like. I want to be able to change the LOGO using Umbraco's media picker. However, everytime I do so, it either gives me the alt name or a runtime error.
This is the code for the image on the template.
<img src="#Umbraco.Field("navigationLogo")" height="100" width="50" alt="Logo">
I'd appreciate it if you can help me. I'm not sure if I've explained it properly so feel free to ask in detail. Thanks!
Edit -----
I am only coding on browser since it doesn't allow me to login to pull the codes locally (only on trial version since I'm just testing out if its a good CMS).
Not sure if I'm doing it correctly so I apologize. I wrote my code like this
#{
var navigationLogo = #Umbraco.AssignedContent.Site().GetPropertyValue("navigationLogo");
var navigationLogoMediaItem = #Umbraco.TypedMedia(navigationLogo);
<img src="#navigationLogoMediaItem.Url" height="100" width="50" alt="Logo" />
}
It's giving me the following error so far.
Compiler Error Message: CS1061: 'Umbraco.Web.UmbracoHelper' does not contain a definition for 'AssignedContent' and no extension method 'AssignedContent' accepting a first argument of type 'Umbraco.Web.UmbracoHelper' could be found (are you missing a using directive or an assembly reference?
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "WebBase.cshtml";
string navigationLogoMediaItem = "";
if (Model.Content.HasValue("navigationLogo"))
{
navigationLogoMediaItem = Model.Content.GetPropertyValue<IPublishedContent>("navigationLogo").Url;
}
}
<img src="#navigationLogoMediaItem" height="100" width="50" alt="Logo" />
you can try this
I'm going to make a few assumptions so please bear with me here.
Assumption 1) On your home node (assuming a document type of 'home") you have a property on it called "navigationLogo".
Assumption 2) This is a media picker data type
Assumption 3) You are using Umbraco 7.6 or newer.
Assumption 4) You aren't using Models Builder
So, assumptions in place there are a few steps you need to go through.
1) Get the navigation logo where ever you are in the site.
var navigationLogo = #Umbraco.AssignedContent.Site().GetPropertyValue("navigationLogo");
2) next, you'll need to get the media item as I belive you will only have a UDI at this stage.
var navigationLogoMediaItem = #Umbraco.TypedMedia(navigationLogo);
3) You'll want to put a null check in at this point but to get the url of the logo you simple need to do this:
<img src="#navigationLogoMediaItem.Url" height="100" width="50" alt="Logo" />
That should get you what you are after. :-)
You can simply try out this code.
memberImage is your document type field name
#{
var image = Umbraco.TypedMedia(#Umbraco.Field("memberImage").ToString());
<img src="#image.Url" style="max-width:250px">
}

How to show umbraco multiple media picker images on page with a macro

Hello stackoverflow people
hope you can help me with maybe a simple question, but couldn't find a solution elsewhere and I have just been working with umbraco for a week now and have never used the mvc part before so all is new for me.
So the big problem is how I make a macro to show these images I choose from the multiple media picker the macro should just end with showing.
<img src="img1.gif" height="50" width="50">
<img src="img2.gif" height="50" width="50">
And so on depending on how many images there is. (the size is just an exempel)
I tryed somthing like this
#var selectedMedia3 = #Library.MediaById(Model.mainImage);
<img src="#selectedMedia3.umbracoFile" width="#selectedMedia3.umbracoWidth" height="#selectedMedia3.umbracoHeight" alt="#selectedMedia3.Name"/>
}
But I dont know how to parse the id of the image to the macro.
and when I choose more than one file I need a loop, but dont know how to loop the multiple media picker data, so im a little lost by now.
Are you able to let us know what version of Umbraco you are using. Umbraco has gone through a number of fundemental changes in various version over recent years. The below code should guide you in the right direction for Umbraco 7 Multiple Image picker with the propertyAlias partnersLogos.
#if (Model.Content.HasValue("partnersLogos"))
{
var partnersImagesList = Model.Content.GetPropertyValue<string>("partnersLogos").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var partnersImagesCollection = Umbraco.TypedMedia(partnersImagesList).Where(x => x != null);
foreach (var partnerImage in partnersImagesCollection)
{
<img src="#partnerImage.Url" alt="partners logo" />
}
}
If anyone makes the same mistake as me and doesn't realise that there is a difference between the now obsolete media picker and the new media picker "Umbraco.MediaPicker2" (true from at least 7.6.1) then please read the documentation on Umbraco's web site.
https://our.umbraco.org/documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker2
#{
var typedMultiMediaPicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("sliders");
foreach (var item in typedMultiMediaPicker)
{
<img src="#item.Url" style="width:200px"/>
}
}
I'm not really sure if you question is how to setup MVC within umbraco or getting values from an image picker.
But if you want to start up with MVC in umbraco check this out: http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/

Umbraco 4.11 MVC Mode - Children Url?

What is the correct way to obtain the URLs of children of the current node while using Umbraco 4.11 in MVC mode? This is what I tried and it keeps returning a blank URL:
#if (CurrentPage.Children().Count() > 0) {
<ul>
#foreach (var child in CurrentPage.Children()) {
<li>#child.Name</li>
}
</ul>
}
When that didn't work I resorted to the following:
#Umbraco.NiceUrl(child.Id)
This returns a URL, but it has .aspx extension on it. So my next hack is...
#Umbraco.NiceUrl(child.Id).Replace(".aspx", "")
That's not completely terrible, but I'm wondering if I'm missing something?
You need the NiceUrl() method to generate a valid Url.
To get rid of the .aspx extension, you need to set the setting "umbracoUseDirectoryUrls" to true in your web.config file:
<add key="umbracoUseDirectoryUrls" value="true" />
I can't add this as a comment, but there is a way to remove trailing slashes, in umbracoSettings.config:
<addTrailingSlash>true</addTrailingSlash>
Other way to get Only Node Name (Without .aspx) is
#Model.NodeById(#child.Id).urlName
But it will just give Node name without "/"

Resources