How to get CSS file with WebMvcConfigurer in Spring - html

I will show you my code and project structure then what I tried so far:
WebMvcConfigurer:
#Configuration
public class ResourceHandlers implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/*").addResourceLocations("/resources/css/");
}
}
This is in my .jsp page: <spring:url value="/resources/css/homepage.css" var="homePageCss" />
This is my project strucutre:
.idea
.lib
-src
-main
.java
- resources //this is automatically created folder resources
- css
- homepage
.homepage.css
-webapp
- resources
- css
- homepage
.homepage.css
In this project structure that I showed I tried both ways. Manually create resource folder inside webapp and then create folder css, homepage, and then css page.
Also I tried to create css, homepage and the css page inside my automatically created folder resources but both ways doesnt work.

Also add #EnableWebMvc Annotation
Try This
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/css/homepage/");
}
JSP
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%> //Add at the top the page
<%#page isELIgnored="false"%> //Add at the top the page
<spring:url value="/resources/homepage.css" var="homePageCss" />
<link href="${homePageCss}" rel="stylesheet" />

Related

How to add css file to cshtml file?

I've been trying to add a css file to style a cshtml file, and so far I've been unsuccessful.
Here's what I've tried to do:
#model mvc.Models.LoginViewModel
#{
ViewData["Title"] = "Login";
}
<head>
<div class="text-center">
<h1 class="display-4">Login Page</h1>
</div>
<link rel="stylesheet" href="~/Content/login.css" type="text/css" />
</head>
Here is the directory tree with the two files I'm trying to link
Edit: when I put the css styling in a "style" tag in the cshtml then it works. I was hoping there was a way to do it with a css file.
Can you please help me?
Thank you.
From your project structure, I think you may misunderstand asp.net and asp.net core. .NET 5 is the next version of .NET Core.
You use .NET 5.0, so if you want to directly add css reference, you need add these static files to wwwroot folder in root project. Be sure you have added static files middleware in Startup.cs:
app.UseStaticFiles(); //be sure add this...
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
Then add reference like below:
<link rel="stylesheet" type="text/css" href="~/login.css" />
Reference: Serve files in web root
If you put static files to other folder(e.g Content folder) which is in root project, you need add the following code to Startup.cs first:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseHttpsRedirection();
app.UseStaticFiles(); // For the wwwroot folder.
// using Microsoft.Extensions.FileProviders;
// using System.IO;
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "Content")),
RequestPath = "/StaticFiles",
EnableDirectoryBrowsing = true
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Add static files reference like below:
<link rel="stylesheet" type="text/css" href="/StaticFiles/login.css" />
Reference: Serve files outside of web root

SSRS report server Data issue

I have deployed SSRS report on Production environment. When 3 or more user run the same report on production then report server load other user requested data.
For Example-
Below 3 request-
User A requested Report X with Paremeter A
User B requested Report X with Paremeter B
User C requested Report X with Paremeter C
Output from SSRS server -
User A requested Report X with Paremeter A
User B requested Report X with Paremeter A
User C requested Report X with Paremeter C
Note- This problem occur when all the users request report in same time.
We had same issue, if you are using ReportViewerForMVC from NuGet Package manager which is third party control may cause problem, follow below points
we will re-use code from ReportViewerForMvc and modify as per our need
1
Install-package ReportViewerForMvc and press enter. After few minutes the package will be installed
2
This installation will add to the project: 2 assemblies (Microsoft.ReportViewer.WebForms & ReportViewerForMvc)to references an .aspx page (ReportViewerWebForm.aspx) and httphandlers settings in the web.config file.
Note: the .aspx page added does not have a .cs file.
You can now use this .aspx page and code everything in controller, but I am using a slightly different path for code reusability and consistency)
Add a new folder ‘Reports” to the project, and then add a new webform .aspx page (ReportTemplate.aspx to the Reports folder
3
Copy the contents (as shown in fig) from ReportViewerWebForm.aspx and replace the content of ReportTemplate.aspx with this.
Note: Please do not copy #page directive, copy only the highlighted section.
4
<%# Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">--%>
<!doctype html>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManagerReport" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer runat ="server" ShowPrintButton="false" Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >
</rsweb:ReportViewer>
</div>
</form>
</body>
</html>
5
The ReportTemplate.aspx will change to this
6
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ReportTemplate.aspx.cs" Inherits="ASPNETMVC_SSRS_Demo.Reports.ReportTemplate" %>
<%# Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">--%>
<!doctype html>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManagerReport" runat="server">
<Scripts>
<asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
</Scripts>
</asp:ScriptManager>
<rsweb:ReportViewer runat ="server" ShowPrintButton="false" Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >
</rsweb:ReportViewer>
</div>
</form>
</body>
</html>
7
Next delete the below scripts tag from ReportTemplate.aspx page
<Scripts>
<asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
</Scripts>
8
Add additional attributes to the ReportViewercontrol as below
<rsweb:ReportViewer id="rvSiteMapping"runat ="server"ShowPrintButton="false"Width="99.9%"Height="100%"AsyncRendering="true"ZoomMode="Percent"KeepSessionAlive="true"SizeToReportContent="false"></rsweb:ReportViewer>
9
Now open ReportTemplate.aspx.cs file and add below code to the Page_load event, you need to the SSRS Server URL and SSRS report Folder path.
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPNETMVC_SSRS_Demo.Reports
{
public partial class ReportTemplate : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
String reportFolder = System.Configuration.ConfigurationManager
.AppSettings["SSRSReportsFolder"].ToString();
rvSiteMapping.Height =
Unit.Pixel(Convert.ToInt32(Request["Height"]) - 58);
rvSiteMapping.ProcessingMode =
Microsoft.Reporting.WebForms.ProcessingMode.Remote;
rvSiteMapping.ServerReport.ReportServerUrl = new Uri("SSRS
URL"); // Add the Reporting Server URL
rvSiteMapping.ServerReport.ReportPath =
String.Format("/{0}/{1}", reportFolder, Request["ReportName"].ToString());
rvSiteMapping.ServerReport.Refresh();
}
catch (Exception ex)
{
}
}
}
}
}
10
Add the SSRSReportFolder path to the app settings in the web.config file.
<add key="SSRSReportsFolder" value="BIC_Reports"/>
11
Next, create an entity class ReportInfo.cs under Models folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ASPNETMVC_SSRS_Demo.Models
{
public class ReportInfo
{
public int ReportId { get; set; }
public string ReportName { get; set; }
public string ReportDescription { get; set; }
public string ReportURL { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string ReportSummary { get; set; }
}
}
12
Next, we will add code to the Controller and the View Pages. There is no
change to the HomeController.cs. Add the following code to Home/Index view
page.
#{
ViewBag.Title = "Index";
}
<h2>Reports List</h2>
<a id="ReportUrl_Performance" href="#Url.Action("ReportTemplate", "Report", new { ReportName = "Performance", ReportDescription = "Performance Report", Width = 100, Height = 650 })">
Performance Report</a>
13 Next
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPNETMVC_SSRS_Demo.Models;
namespace ASPNETMVC_SSRS_Demo.Controllers
{
public class ReportController : Controller
{
//
// GET: /Report/
public ActionResult ReportTemplate(string ReportName, string
ReportDescription, int Width, int Height)
{
var rptInfo = new ReportInfo
{
ReportName = ReportName,
ReportDescription = ReportDescription,
ReportURL = String.Format("../../Reports/ReportTemplate.aspx?
ReportName={0}&Height={1}", ReportName, Height),
Width = Width,
Height = Height
};
return View(rptInfo);
}
}
}
14
Final Step is to open the ReportTemplate view page under Report and add the following code.
#model ASPNETMVC_SSRS_Demo.Models.ReportInfo
<H1>
#Model.ReportDescription
</H1>
<iframe id="frmReport" src="#Model.ReportURL" frameborder="0" style="#String.Format("width:{0}%; height: {1}px;", Model.Width, Model.Height)" scrolling="no">
</iframe>

how can display html page in action

my project structure :
RouteConfig :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
index.html :
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h3>hi</h3>
</body>
</html>
Home Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
WebApi is startup project.
how can display index.html in Index action or replace Index view with index.html.
The files are in different profects, you can´t do it at this way.
Why you dont use the home/index action and views/home/index.cshtml to put your index.html content?
Or you can make a redirect to index.html URL on home/index action.
By default, your controller action will return Index.cshtml view instead of Index.html page. If you want to do redirection pointed at Index.html on WebClient project, modify your Index action method inside controller like this (set your WebClient's full address path, because tidle sign to replace site root path doesn't work at this case):
public class HomeController : Controller
{
public ActionResult Index()
{
return Redirect("http://ServerAddressOrName:Port/WebClient/Index.html");
}
}
Afterwards, you need to build both projects then assign your WebApi and WebClient to different sites, since it is impossible to combine them with different Web.config content.

Url Route HTML not rendering

I have have using Url Route but sometimes I feel like the HTML code is not rendering
This is my code in Global.asax
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("LogIn", "logga-in", "~/Login.aspx");
routeCollection.MapPageRoute("Contact", "kontakt", "~/Contact.aspx");
routeCollection.MapPageRoute("About", "om", "~/About.aspx");
routeCollection.MapPageRoute("Job", "jobb", "~/Job.aspx");
routeCollection.MapPageRoute("Connect", "anslut", "~/Connect.aspx");
routeCollection.MapPageRoute("Default", "", "~/Default.aspx");
routeCollection.MapPageRoute("Booking", "bokning/{Id}", "~/Booking.aspx");
routeCollection.MapPageRoute("DashboardDefault", "panel/", "~/Dashboard/Default.aspx");
routeCollection.MapPageRoute("DashboardAbout", "panel/om", "~/Dashboard/About.aspx");
routeCollection.MapPageRoute("DashboardContact", "panel/kontakt", "~/Dashboard/Contact.aspx");
routeCollection.MapPageRoute("DashboardLogIn", "panel/logga-in", "~/Dashboard/Login.aspx");
}
When going to the aspx site everyting is working fine ex http://www.dentalo.se/dashboard/login.aspx
se image below
Trying to access the same page but with friendly url instead http://www.dentalo.se/panel/logga-in
it looks like this.
Can anyone help me why this happens? I know for sure that the javascript is not rendering also.
Your css style-sheets are not loading. This happens because of the rewriting and the browser tries to load the file from the path /panel/assets/... Use absolute path to make things work.
<link href="/dashboard/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link href="/dashboard/assets/css/metro.css" rel="stylesheet" />
...

How to make an iframe which displays the parent page

I want to display an iframe in a .aspx page, and the iframes source should be the same page.
I need to use a relative uri.
What value should I give the 'src' attribute?
I realise this is a little unusual - the page will be displayed in different states depending on parameters passed in, so the iframe won't be displayed within itself.
If you do this you will get an endless loop... the processsing will "never end". maybe thats why it is white? it is really processing pages..
- is that what you want ? if you for example want just 2-3 pages in depth, you can youse querystring and for example disable the iframe when the querystrings are incremented to 3.
MyPage.aspx?depth=1 --MyPage.aspx?depth=2 --MyPage.aspx?depth=3 etc
The literal relative path should work. IE: MyPage.aspx
Here is an ASP.NET Example...
Seemed to work fine for me with the following...
Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<iframe runat="server" id="myFrame" src="Default.aspx?message=Hello%20World"></iframe>
<div id="myDiv" runat="server"></div>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string message = Request.QueryString["message"];
if (null != message)
{
myDiv.InnerText = message;
myFrame.Visible = false;
}
else
{
myDiv.Visible = false;
}
}
}
}
The short answer is src="localfilename.aspx" within the iframe tag. The web standard, loosely applied, says anything not proceeded by a '/' is relative to the location of the current page. Sometimes src="" might even work for substituting the current file name (at the browser level)