making ajax call from within bootstrap modal dialog on change of an element - bootstrap-modal

$('#PolicyTypeId').change(function () {
var url = '#Url.Action("GetTerm", "InsuranceSetup")';
var value = $('#PolicyTypeId').val();
var age = $('#Age').val();
$.getJSON(
url,
{ age: age, plantype: value },
function (data) {
if (data.result == "") {
Notify('There was a problem with the term', 'top-left', '5000', 'danger', 'fa-desktop', true);
}
$('#Term').val(data.result);
}
);
return false;
});
This code is supposed to make an ajax call to get some data from a controller upon a value change on a dropdown element. However, the url is changed to the below url for the request. The customerdetails/edit/id is the url from which the modal dialog is rendered with a partial view that has a form inside.
http://localhost:44379/customerdetails/edit/#Url.Action(%22GetTerm%22,%20%22InsuranceSetup%22)?age=25&plantype=17

Assuming the code you posted is in the view, it looks like Url.Action isn't working. Make sure you have the required settings in web.config:
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="WebApplication1" />
</namespaces>
</pages>
</system.web.webPages.razor>
That's normally in the Views folder.
If the code is in a JavaScript file, the url helper won't actually do anything there.

Related

ASP proper config to allow CORS in a asmx webservice for POST requests

I´m trying to setup a server capable to handle CORS, but also capable to return the response in json format.
Tried with various config schemes, but no luck.
I need to do my Ajax requests via POST. With my actual config i´m able to do requests via GET, but currently my response always is encapsulated in XML format.
As stated in ResponseFormat.Json returns xml , my requests must be 'application/json' but when I try to do it in this way I'm receiving a preflight message (Response for preflight has invalid HTTP status code 404).
My web.config (allowing CORS via GET) currently is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2">
<assemblies>
<add assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<add assembly="CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<!--<add assembly="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>-->
<add assembly="CrystalDecisions.ReportSource, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<add assembly="CrystalDecisions.ReportAppServer.Controllers, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<add assembly="CrystalDecisions.ReportAppServer.DataDefModel, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
<add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
</assemblies>
</compilation>
<httpRuntime />
<pages controlRenderingCompatibilityVersion="4.0" />
<httpHandlers>
<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
</httpHandlers>
<sessionState timeout="30"></sessionState>
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
<appSettings>
<add key="CrystalImageCleaner-AutoStart" value="true" />
<add key="CrystalImageCleaner-Sleep" value="60000" />
<add key="CrystalImageCleaner-Age" value="120000" />
</appSettings>
<connectionStrings>
<....>
</connectionStrings>
<system.webServer>
<handlers>
<add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" /><remove name="OPTIONSVerbHandler" /><remove name="TRACEVerbHandler" /><add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /></handlers>
<validation validateIntegratedModeConfiguration="false" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="X-Requested-With, Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
I´m using iron-polymer for the Ajax-requests and combined various content-types. Currently i´m getting my data via GET using 'content-type="application/x-www-form-urlencoded; charset=UTF-8" handle-as="document"'.
I´m having difficulties to process the response due such XML encapsulation. Below are two examples of received responses. How to extract the contents ?
1:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://my.domain/">EF7B48E346BC537F98F9696304CECAFE</string>
2:
'<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCiudad xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://my.domain/">
<Ciudad>
<Codigo>32</Codigo>
<Nombre>BOGOTA</Nombre>
<CodigoDepartamento>Z03</CodigoDepartamento>
</Ciudad>
<Ciudad>
<Codigo>133</Codigo>
<Nombre>CALI</Nombre>
<CodigoDepartamento>Z01</CodigoDepartamento>
</Ciudad>
<Ciudad>
<Codigo>53</Codigo>
<Nombre>CARTAGENA</Nombre>
<CodigoDepartamento>Z13</CodigoDepartamento>
</Ciudad>
<Ciudad>
<Codigo>30</Codigo>
<Nombre>MEDELLIN</Nombre>
<CodigoDepartamento>Z04</CodigoDepartamento>
</Ciudad>
</ArrayOfCiudad>'

ELMAH MVC Route not displaying

I just installed ELMAH.Mvc and left all the default configurations and I am able to get to the elmah route and see my log. however, when i deploy my application I get a 403 Forbidden Access page. I thought since I had the requiresAuthentication flag set to false it would be available. Here are my settings:
<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
<add key="elmah.mvc.requiresAuthentication" value="false" />
<add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
<add key="elmah.mvc.allowedRoles" value="*" />
<add key="elmah.mvc.allowedUsers" value="*" />
<add key="elmah.mvc.route" value="elmah" />
<add key="elmah.mvc.UserAuthCaseSensitive" value="true" />
any help would be great!
doh! I needed to add this to my web.config:
<security allowRemoteAccess="yes" />

Required dependency of type Elmah.Mvc.ElmahController could not be resolved

Elmah is logging errors properly to my database but I can't get to /elmah. What am I missing? This was working without ever implementing a controller for Elmah, but now it's not working. This is following a git merge. All configuration has been reset to how it was working before.
<system.web>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
<appSettings>
<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
<add key="elmah.mvc.requiresAuthentication" value="false" />
<add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
<add key="elmah.mvc.allowedRoles" value="*" />
<add key="elmah.mvc.allowedUsers" value="admin" />
<add key="elmah.mvc.route" value="elmah" />
</appSettings>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="[MyCSName]" />
</elmah>
</system.webServer>
I realise this is an old question but it was the top google search result.
Another option if you want to continue using Funq is pass the additional assemblies you want scanned to funq.
Change
new ServiceStack.Mvc.FunqControllerFactory(container);
To
new FunqControllerFactory(container, typeof(ElmahLogger).GetAssembly(), typeof(ElmahController).GetAssembly())
I am also using ServiceStack, this is what worked for me:
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container,
typeof (ElmahController).Assembly));
This most likely happens because you are using ServiceStack Funq IOC.
In App_Start\AppHost.cs (name may varies), try commenting out this line:
//Set MVC to use the same Funq IOC as ServiceStack
//ControllerBuilder.Current.SetControllerFactory(new ServiceStack.Mvc.FunqControllerFactory(container));
Regards.

VS2012 Unit Test Project and MSFT Ent Lib 5.0

I have the following (but very annoying) problem with this particular type project and Ent Lib 5.0.
The reason I am saying it pertains to the fact that it is a Unit Test project related is that the same code/configuration work fine from a Console Application.
Here is the code:
ICacheManager cm = CacheFactory.GetCacheManager("TestCacheManager");
cm.Add("max", 1);
Here is the config:
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="Caching" />
<connectionStrings>
<add name="Caching" connectionString="Data Source=.;Initial Catalog=Caching;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<cachingConfiguration defaultCacheManager="TestCacheManager">
<cacheManagers>
<add name="TestCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="TestStore" />
</cacheManagers>
<backingStores>
<add name="TestStore" type="Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
encryptionProviderName="" databaseInstanceName="Caching"
partitionName="Name" />
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
</configuration>
And then finally the exception that I only get in the Unit Test Project:
Activation error occured while trying to get instance of type ICacheManager, key "TestCacheManager"

How do you configure Elmah to tweet error messages?

I read that Elmah can tweet error messages so I tried to set it up, but could not get it to work. I am able to get it to save the errors into a database and email me the error, but the tweet doesn't seem to work.
Here is my configuration:
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.SqlServerCompactErrorLog, Elmah" connectionStringName="LoggingDB" />
<errorTweet
userName="twitter_username"
password="twitter_password" statusFormat="{Message}" />
</elmah>
<system.webServer>
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
No errors occur, the tweet just does not appear even though it's logged in the database and emailed.
See the ErrorHowling wiki on the project's site for some background and information on ErrorTweetModule and its use.